CakePHP的2.0对象不数组 [英] CakePHP 2.0 Object not Array

查看:171
本文介绍了CakePHP的2.0对象不数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在CakePHP的初学者,并与CakePHP的1.3发挥各地,但最近CakePHP的2.0已经发布。

I am currently a beginner in CakePHP, and have played around with CakePHP 1.3, but recently CakePHP 2.0 has been released.

到目前为止,我喜欢它,但唯一的事是一个痛苦的事实是,它不返回对象,而它只是返回数组。我的意思是,它几乎是有道理不得不做$信息['邮政'] ['身份证']。这是(在我看来)更实际的,只是做后$> ID。

So far I like it but the only thing is being a pain is the fact that it doesn't return Objects, rather it just returns arrays. I mean, it hardly makes sense to have to do $post['Post']['id']. It is (in my opinion) much more practical to just do $post->id.

现在谷歌后,我偶然发现的链接,但是,这些都使生成有关索引错误没有被使用Form类定义时(猜测这是因为它是获得客观的版本,而不是数组版本)。

Now after Google I stumbled upon this link, however, this kept generating errors about indexes not being defined when using the Form class (guessing this is because it was getting the objectified version rather than the array version).

我下面的博客教程(已经跟着它1.3下却又在它去为2.0)

I am following the Blog tutorial (already have followed it under 1.3 but going over it again for 2.0)

那么,谁知道如何没有它使用Form类的干扰来实现这一目标?

So, anyone know how to achieve this without it interfering with the Form class?

围场

推荐答案

您可以创建额外的对象瓦尔。这样,你就不会与蛋糕的干扰AUTOMAGIC,但可以使用格式例如 $ modelNameObj-> ID访问数据。格式

You could create additional object vars. This way you wouldn't interfere with Cake's automagic but could access data using a format like $modelNameObj->id; format.

首先,在/ app /控制器创建AppController.php,如果你不已经有一个。然后创建一个beforeRender()函数。这将查找在蛋糕的标准命名约定的数据,并从它创建额外的对象瓦尔。

Firstly, create an AppController.php in /app/Controller if you don't already have one. Then create a beforeRender() function. This will look for data in Cake's standard naming conventions, and from it create additional object vars.

<?php
App::uses('Controller', 'Controller');

class AppController extends Controller {

  public function beforeRender() {

    parent::beforeRender();

    // camelcase plural of current model
    $plural = lcfirst(Inflector::pluralize($this->modelClass));

    // create a new object
    if (!empty($this->viewVars[$plural])) {      
      $objects = Set::map($this->viewVars[$plural]);
      $this->set($plural . 'Obj', $objects);     
    }

    // camelcase singular of current model
    $singular = lcfirst(Inflector::singularize($this->modelClass));    

    // create new object 
    if (!empty($this->viewVars[$singular])) {      
      $object = Set::map($this->viewVars[$singular]);
      $this->set($singular . 'Obj', $object);
    }
  }
}

然后在你的意见,你可以访问像这样的对象:

Then in your views you can access the objects like so:

index.ctp

$productsObj;

view.ctp

$productObj->id;

我们所要做的是增加的OBJ到蛋糕应该已经提供的变量名。一些示例映射:

All we're doing is adding 'Obj' to the variable names that Cake would already provide. Some example mappings:

产品 - > $ productsObj

ProductType - > $ productTypesObj

ProductType -> $productTypesObj

我知道这并不完美,但它本质上会达到你想要的东西,并会在所有的车型可供选择。

I know this is not perfect but it would essentially achieve what you wanted and would be available across all of your models.

这篇关于CakePHP的2.0对象不数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆