going
going

Reputation: 9817

Using read() in cakephp to retrieve row with array of data

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.

Upvotes: 3

Views: 8037

Answers (4)

Dave
Dave

Reputation: 29131

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));
}

Upvotes: 0

Nick Zinger
Nick Zinger

Reputation: 1174

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));
}

Upvotes: 0

Dunhamzzz
Dunhamzzz

Reputation: 14798

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.

Upvotes: 4

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

In model you could do:

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

Hope it helps

Upvotes: 1

Related Questions