Was considering utilizing the magic findBy
functions on a model today and encountered an issue when attempting to set optional parameters for the function. Here is what I tried.
正在考虑今天在模型上使用魔术findBy函数,并在尝试为函数设置可选参数时遇到问题。这是我尝试过的。
$result = $this->findById($id['Alpha.name']);
So to explain, I'm attempting to find a record with a specific id
and only return the value of the name
field. According to the documentation, there is a way to do this.
所以要解释一下,我试图找到一个具有特定id的记录,只返回name字段的值。根据文档,有一种方法可以做到这一点。
The findBy magic functions also accept some optional parameters:
findBy<fieldName>(string $value[, mixed $fields[, mixed $order]]);
findBy魔术函数也接受一些可选参数:findBy
(string $ value [,mixed $ fields [,mixed $ order]]);
CakePHP 1.3 Book :: findBy
When I do a simple findBy($id)
I do get a result set. But with parameters, I get nothing. I know there are other ways to do this but was just curious if anyone has had any success using these magic functions with additional parameters?
当我做一个简单的findBy($ id)时,我得到一个结果集。但有了参数,我什么都没得到。我知道还有其他方法可以做到这一点,但只是好奇,如果有人使用这些神奇功能与其他参数取得任何成功?
1 个解决方案
#1
11
try this:
尝试这个:
$result = $this->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
)
其中$ id是您要搜索的记录的ID,Alpha.name是您需要的字段(例如,来自模型Alpha的名称)
#1
11
try this:
尝试这个:
$result = $this->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
)
其中$ id是您要搜索的记录的ID,Alpha.name是您需要的字段(例如,来自模型Alpha的名称)