开发者

find by column name cakePHP

开发者 https://www.devze.com 2023-01-19 23:58 出处:网络
Hello I have been trying to understand how to get data from model by the name of field. I am using cakePHP, and I need to retreive a column\'s data from a table. The syntax is

Hello I have been trying to understand how to get data from model by the name of field. I am using cakePHP, and I need to retreive a column's data from a table. The syntax is

> "select name from permissions"

So I tried to find out on book.cakephp.org, so I got the field function, but that only gives me the first value, while I have more than one values for this.

I tried do a

$this->Model->find(array('fields'=>'Model.fieldName'));

but I understood that the syntax itself is flawed.

Can somebody let me know w开发者_运维问答hat is the method to query based on column name.


$this->Model->find(array('fields'=>'Model.fieldName'))

You forgot the array function. Also:

$this->Model->find(array('fields'=>array('Model.fieldName')))

will work.


findAllBy will find all records based on the field name.

$this->Model->findAllBy<fieldName>(string $value, array $fields, array $order, int $limit, int $page, int $recursive);

For eaxample:

$this->Permission->findAllByName('Some Name');

http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#findallby


Found it... hope it will help someone.

    $workshop_lists = ClassRegistry::init('Workshop')->find('all',array(
    'fields'=>array('user_id', 'title')
    ), 
     array(
       'conditions' => array('user_id' => $this->Auth->user('id')),
       'group' => 'Workshop.user_id',
        'order' => 'posted DESC',
    ));


There is no way you can query out based on column name using one of the cake methods. You have to use the query method.

Syntax: $this->Model->('Select columnname from table');


$this->Model->find('all',array('fields'=>array('Model.fieldName')))

it works for me everytime.


If I understood well and you want not only 1 value but the whole values in the column 'name' from the table 'permissions'. In that case you could use:

$this->Model->find('list',$params);  

(see explanation for 'find' here)

for the '$params' part you would use:

$params=array('fields'=>array('name'));

or putting all in a single line:

$arrayOfNames= $this->Model->find('list',array('fields'=>array('name')));

This will give you an array '$arrayOfNames' wich key is the 'id' (primary key) in 'permissions' table and wich value is the corresponding name in the field 'name' from the same table. This is the array would be something like: 'id'=>'name'

[23]=>'name1'
[28]=>'name2'
[29]=>'name3'
............

very much like I think you want. Hope it helps.


$this->Model->find('list', ['valueField' => 'fieldName']);
0

精彩评论

暂无评论...
验证码 换一张
取 消