CakePHP 2.0.4 - findBy magic methods with conditions

女生的网名这么多〃 提交于 2019-12-11 03:25:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!