3

I want to know if it is possible to retrieve a row from the database using something similar to the following:

    if (!empty($this->params['form'])) {
        $place = array();
        $place['city'] = $this->params['form']['city'];
        $place['area'] = $this->params['form']['state'];
        $place['country'] = $this->params['form']['country'];
        $place['iso'] = $this->params['form']['iso'];

        $this->Place->set($place);
        $place_found = $this->Place->read();
    }

Is there some way I can preset the data in the Place model using the array and then use Place read. I'm looking for something simple like the usual:

$this->Place->id = 7;
$place_found = $this->Place->Read();

I have also tried doing this:

$this->Place->city = blah;
$this->Place->area = foo; etc....

$place_found = $this->Place->read();

However, that also does not work.

4 Answers 4

4

Haven't you ever used find()?! read() only fetches a row with the ID passed.

$place_found = $this->Place->find('first', array(
    'conditions' => array(
          'Place.city' => $city,
          'Place.area' => $area
          // etc
     )
));

If you need to build the conditions manually you can create a conditions array to pass like so:

$placeConditions = array();
$placeConditions['city'] = $city;
if($area) {
    $placeConditions['area'] = $area;
}

$places = $this->Place->find('first', array('conditions' => $placeConditions));

I suggest you read the page I linked, you will soon find out there is never a reason to use the read() method.

Sign up to request clarification or add additional context in comments.

4 Comments

I am looking for an easier way to do this by using the array without having to write everything out in full. I am trying to find out if cake has an easier way to do this. You will notice if you use $this->Place->set($place) it actually set the Place->data to the value provided, I'm pretty sure there exists a method to then retrieve the missing values but I can't remember what it is.
Please see updated answer, read() can only retrieve rows based on an ID.
Have to agree with Dunhamzzz. The analog for doing a SELECT query with conditions in a WHERE clause is to use find(). Consider read() a special case that only queries by ID and automatically populates the model instead of returning an array.
This is probably a misconception on my part, looking at this again today and looking at your answer they are essentially the same amount of coding. Thank you.
1

In model you could do:

$this->id = 3; //place id
$this->Place->read();

Hope it helps

1 Comment

I don't have the id available, I am trying to find based on other conditions
0

I think this approach is what you're looking for(with your code):

if (!empty($this->params['form'])) {
    $place = array();
    $place['city'] = $this->params['form']['city'];
    $place['area'] = $this->params['form']['state'];
    $place['country'] = $this->params['form']['country'];
    $place['iso'] = $this->params['form']['iso'];

    //$this->Place->set($place); //don't need it here I think
    $place_found = $this->Place->find('all',array('conditions'=>$place));
}

Comments

0

You'll have to use "find()", not "read()", but - it's almost as simple as your example code and should work. (also, there's a shortcut for the array_push() I believe, but - I like to use this for readability - personal preference):

if (!empty($this->params['form'])) {
    $conditions = array();
    array_push($conditions, array('Place.city' => $this->params['form']['city']);
    array_push($conditions, array('Place.state' => $this->params['form']['state']);
    array_push($conditions, array('Place.country' => $this->params['form']['country']);
    array_push($conditions, array('Place.iso' => $this->params['form']['iso']);

    $this->Place->set($place);
    $place_found = $this->Place->find('all', array('conditions'=>$conditions));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.