follows:
[code]
$rowCount = $this->Room->find(
'count', array(
'conditions' => array(
'Room.location_id' => $id
)
)
);
if ( $rowCount == 0 ) {
// Delete can take place, no children rows!
} else {
// Delete can not take place, has children rows!
}
[/code]
You should move the code to the model, so as to keep your controllers
lean and fast.
Another possibility is for you to use the counter cache, so that for
each location, there will be a counter stating how many rooms are
attached/related to it. Then you just need to check the counter
instead of querying the database.
See http://book.cakephp.org/view/816/counterCache-Cache-your-count for
more information on that!
Enjoy,
John
On Apr 2, 9:56 pm, sebb86 <kahlc...@googlemail.com> wrote:
> Hello,
>
> In an example, i want to delete a location if no rooms are associated
> to this location.
> I tryed the following:
>
> [code]
> function delete($id)
> {
> if(empty($this->Room->find('all', 'fields' =>
> array('Room.location_id'))))
> {
> $this->Location->del($id);
> $this->Session->setFlash('Löschvorgang (id: '.$id.')
> erfolgreich!');
> $this->redirect(array ('action' => 'index'));
> }
> else
> {
> $this->Session->setFlash('Löschvorgang (id: '.$id.')
> fehlgeschlagen, da noch Räume für dieses Gebäude existieren!');
> $this->redirect(array ('action' => 'index'));
> }
> }
> [/code]
>
> Well, this dont works. I dont know how to correctly create the
> "find" :/
> Could someone help me please? Thanks!
Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
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 For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
To unsubscribe, reply using "remove me" as the subject.
No comments:
Post a Comment