Thursday, October 18, 2012

Re: How to read an item from an array

Thank you very much!  That works, and yes, I am doing the find before the delete, and then just sending the title through to the flash message.

On Thursday, October 18, 2012 4:26:17 AM UTC-5, Jeremy Burns wrote:
Whichever method you use (see later notes) $title is an array. To see it's structure, type this immediately after the find:

die(debug($title));

That prints $title to screen, and I'm guessing it will look something like this:

Array(
['Project' ] => Array(
['title'] => value
[fieldname] => value
repeat...
)
)

So to get the actual title of the Project you change your code to:

$this->Session->setFlash(__("Project $title['Project']['title'] deleted"));

Notes: your find methods aren't quite right:

$title = $this->Project->read('title'); should be $title = $this->Project->read(null, $id); (where $id is the id of the Project you want to read)
$title = $this->Project->find('first', array('fields' => array('Project.title'), 'conditions' => array('Project.id' => $id))); <= this is the way to go
$title = $this->Project->query("SELECT `title` FROM `Projects` WHERE `id`=$id"); <= the query method is for last-ditch-all-hope-is-lost cases only

Bear in mind that if you've just deleted the Project none of those methods will return an results, so the $title array will be empty. Presumably you're doing the find before the delete.

If you only want to get a single field (rather than a whole record) consider this instead:

$title = $this->Project->field('title', array('Project.id' => $id));  -- then $title will be equal to the title of your project (i.e. it's not an array).

Jeremy Burns
Class Outfit

http://www.classoutfit.com

On 18 Oct 2012, at 10:12:42, Cody Sortore <rebel2...@gmail.com> wrote:

Okay, I'm a bit of a noob, but I've been messing with this all night and cannot figure it out.  I've had this problem before, but just figured out what my problem is, and although this particular issue isn't pressing, because I plan to use a similar item in the future I would like to understand what I'm missing here.

I'm looking for a way to read a single item from the database, I can pass it to the view rather easily, but reading it in the same controller is confounding me.

I've tried:

$title = $this->Project->read('title');
$title = $this->Project->find('first', array('fields' => array('Project.title'), 'conditions' => array('Project.id' => $id)));
$title = $this->Project->query("SELECT `title` FROM `Projects` WHERE `id`=$id");

All I'm trying to do is display a message that says "Hey you deleted this project" later I have

$this->Session->setFlash(__("Project $title deleted"));

However, when the message flashes all I see "Project Array deleted" with all three examples.  I understand what it's doing... I just don't understand why... how do I select the actual "title" from the array, instead of the entire array?

Using CakePHP 2.x if it makes a difference in this instance.


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake...@googlegroups.com.
To unsubscribe from this group, send email to cake-php+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: