Monday, July 14, 2008

Great thing : bindModel

At present moment a have a lot of free time for self improvement and I can learn CakePHP deeper and deeper, as it possible ;-)

In one of my project I have two table with simple relation

Division (id, title) - hasMany Staff
Staff (id, division_id, name, jobtitle..)

So. Now I need to get some info like: I need to get all Staff where birthday today, and get result like:
Array (
   [0] => ["Division"] = Array ...
         => ["Staff"] = [0]
  [1] ...

But I don't want (or cann't) rewrite model fore staff/division. So, What I must doing?

Of cause I must to use this wonderful thing - bindModel ;-)


$this->Division->bindModel(array("hasMany" => array ("Staff" => array (
"conditions" => array ("MONTH (birthday) = " . $month, "DAY (birthday) = " . $day),
"fields" => array (","),
"order" => array("" => "ASC")))));
$staff = $this->Division->find("all");

Also, we can put in field "fields" something like this
"fields" => array ("COUNT ( as count")

And we have array with Division and numbers (count of staff with correct birthday)

Simple? yep. And in good cakephp tradition ;-)

Labels: , , ,


Blogger Vladimir Luchaninov said...

Not so simple and CakePHP-style anymore :)

Use new core Containable behavior

5:15 AM  
Blogger Vlad said...

Behavior is nice.
But without "simple" how to get needing count fields?
I don't want to use query like "SELECT *...."
In cake style?

2:20 PM  
Blogger Jekin said...

conditions" => array ("MONTH (birthday) = " . $month, "DAY (birthday) = " . $day)

You would have performance issue because the the conditios are not parameterized.

4:00 AM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home