-1

find('all') でデータを検索すると、次の戻りデータが得られます。

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 extends AppModel { public $name = 'Account'; }

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が結果のテーブルのIDをカバーしたのはなぜAccountですか??

そして、私のphpにインストールする必要があるphpexpendを誰が教えてくれますか??

4

1 に答える 1

0

コントローラーでモデルをインスタンス化する必要はありません。コードの実行時に遅延ロードされます。

数値インデックスを削除し、単一のレコードを検索するだけの場合は、 を使用する必要がありますfind('first')。これは、結果から数値インデックスを削除する 1 つのレコードのみを返すためです。

あなたのコントローラ、たとえば 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);
  }
}
于 2013-06-10T09:33:11.653 に答える