为什么搜索结果会返回?

时间:2013-06-09 07:19:57

标签: cakephp

当我通过find('all')搜索数据时,它会获得此resturn数据:

Array ( 
 [0] => 
   Array ( 
     [0] => /* why this is not the model name? */
    Array ( 
      [id] => 1 
      [username] => ** 
      [password] => en 
      [memo] => **
    )
  )
)

我怎样才能得到这样的结果?

Array ( 
 [0] => 
   Array ( 
     ['User'] => /* use the model name? */
    Array ( 
      [id] => 1 
      [username] => ** 
      [password] => en 
      [memo] => **
    )
  )
)

我发现了另一个问题:

account table:
id username password
2  admin    123456

email table:
id title content      account_id
1  test  test email   2

这样的模型:     class Account扩展AppModel {         public $ name ='帐户';     }

class Email extends AppModel{
    public $name = 'Email';
    public $belongsTo = array(
        'Account' => array(
            'className' => 'Account',
            'foreignKey' => 'account_id'
        )
    );
}

所以我编码:

App::import('Model','Email');
$this->Email = new Email();
$result = $this->Email->find('all');

我得到的结果是:

Array ( 
 [0] => 
   Array ( 
     [0] =>
        Array ( 
          [id] => 1 
          [username] => admin 
          [password] => 123456 
          [title] => test
          [content] => test email
          [account_id] => 1
        )
  )
)

为什么Email表的id在结果中覆盖了Account表的id?

谁能告诉我需要在我的php中安装哪些PHP支出?

1 个答案:

答案 0 :(得分:0)

您无需在控制器中实例化模型。运行代码时,它们将被延迟加载。

如果要删除数字索引并且只是搜索单个记录,则应使用find('first'),因为这将只返回一条记录,这将从结果中删除数字索引。

例如,在您的控制器,UsersController中,您应该可以这样做。

App::uses('AppController', 'Controller');

class UsersController extends AppController {
  public function index() {
    $users = $this->User->find('all');
    var_dump($users);
  }
}

如果您想在此处加载其他模型,可以使用$this->loadModel()

App::uses('AppController', 'Controller');

class UsersController extends AppController {
  public function index() {
    $this->loadModel('People');
    $users = $this->People->find('all');
    var_dump($users);
  }
}
相关问题