在 CakePHP 中从模型获取数据的最有效方法是什么?

发布于 2024-08-22 06:36:38 字数 867 浏览 7 评论 0原文

我是 CakePHP 的新手,仍在了解基础知识。现在,我对从模型(从另一个链接模型内部)获取一个或多个字段的过程感到有点困惑。

到目前为止,我有这个:

$this->user->id = 123;
$this->User->read();
$field1 = $this->User->data['User']['field1'];
$field2 = $this->User->data['User']['field2'];

这看起来非常冗长。

$this->user->id = 123;
$field1 = $this->User->field('field1');
$field1 = $this->User->field('field2');

看起来不太长,但会产生两个查询。

在这些情况下,我曾经做什么,在做蛋糕之前:

$this->User = new User(123);
$field1 = $this->User->field1;
$field2 = $this->User->field2;

或者当我想打字时:

this->User = new User(123);
$field1 = $this->User->getFieldOne();
$field2 = $this->User->getFieldTwo();

所以,问题是:我是否缺少 CakePHP 中完成此任务的一些魔力,或者我是否有忍受大量打字?

I'm new to CakePHP, and still figuring out the basics. Right now I'm a bit mystified by the process to get one or more fields from a model (from inside another linked model).

So far, I have this:

$this->user->id = 123;
$this->User->read();
$field1 = $this->User->data['User']['field1'];
$field2 = $this->User->data['User']['field2'];

Which seems awfully verbose.

And this:

$this->user->id = 123;
$field1 = $this->User->field('field1');
$field1 = $this->User->field('field2');

Which seems less long, but results in two queries.

What I used to do in these situations, pre-Cake:

$this->User = new User(123);
$field1 = $this->User->field1;
$field2 = $this->User->field2;

or when I felt like typing:

this->User = new User(123);
$field1 = $this->User->getFieldOne();
$field2 = $this->User->getFieldTwo();

So, the question: am I missing some magic in CakePHP by which to accomplish this task, or do I have to live with typing a lot?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溺深海 2024-08-29 06:36:38

你永远不会相信,但有一条捷径:-)

$this->User->find('all', array('fields'=>array('field1', 'field2')));

You will never believe, but there is a short way :-)

$this->User->find('all', array('fields'=>array('field1', 'field2')));
余生一个溪 2024-08-29 06:36:38
$arrayOfFiels = array('field1', 'field2');
$this->User->id = 123;
$userFields = $this->User->read($arrayOfFields);

或者类似于:

$userFields = $this->User->read(null, 123);

在这两种情况下,$userFields 将是一个包含 User #123 数据的数组。在第二个中,由于第一个参数设置为 null,因此将获取所有字段。第二个参数(可选)设置一个 id,也可以像第一个示例一样提前设置。

$arrayOfFiels = array('field1', 'field2');
$this->User->id = 123;
$userFields = $this->User->read($arrayOfFields);

Or something like:

$userFields = $this->User->read(null, 123);

In both cases, $userFields will be an array with User #123 data. In the second one, due to the first argument set as null, all fields will be fetched. Second argument (optional) sets an id, which can also be pre-set earlier like in the first example.

悟红尘 2024-08-29 06:36:38

中获取数据

您可以使用“读取”或“查找”或“查询”从模型读取

$fields = array('field1','field2');
$this->data = $this->User->read( $fields,$someid );

查找

$this->data = $this->User->find('all',array('fields'=>array('field1','field2') );

查询

$this->data = $this->User->query("select field1,field2 from user where id=$someid;");

you can either use 'read' or 'find' or 'query' to get data from model

read

$fields = array('field1','field2');
$this->data = $this->User->read( $fields,$someid );

find

$this->data = $this->User->find('all',array('fields'=>array('field1','field2') );

query

$this->data = $this->User->query("select field1,field2 from user where id=$someid;");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文