问题
I am trying to build a small cms to test CakePHP 2.x
In my PagesController (for displaying single sites), I use this code:
$page = $this->Page->findByNavtitle($name, array(
'conditions' => array(
'Page.visible' => '1',
),
)
);
The result should only set when the record is marked as visible. But this codeblock throws an error.
The API describes, that just one parameter is allowed in these findBy magic methods.
How can I get the result with conditions?
回答1:
You cannot add conditions to findBy method. Instead use find:
$page = $this->Page->find('first', array(
'conditions' => array(
'Page.nav_title' => $name,
'Page.visible' => 1
)
));
Hope it helps
回答2:
$this->Model->findAllBy(string $value, array $fields, array $order, int $limit, int $page, int $recursive);
回答3:
Findbyid in cake php
$result = $this->Modelname->findById($id, array('Alpha.name'));
where $id is an id of record you're searching for and Alpha.name is a field you need (e.g. name from model Alpha)
来源:https://stackoverflow.com/questions/8725495/cakephp-2-0-4-findby-magic-methods-with-conditions