0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【cakephp】setについて

Last updated at Posted at 2020-02-18

Set互換のパス記法

  • {n} 数値添字の表現
  • {s} 文字列添字の表現
  • Foo 任意の文字列(括弧囲み無し) これは文字列リテラルなどとして扱われます。
  • {[a-z]+} 括弧で囲まれた任意の文字列({n}と{s}を除く) は正規表現と解釈されます。

set::combine

$users = $this->User->find( 'all' );
$list = Set::combine( $users, '{n}.User.id', '{n}.User.name' );

$usersは


array(
	[{0}.user.id] => [{0}.user.name]
	[{1}.user.id] => [{1}.user.name]
	[{2}.user.id] => [{2}.user.name]
)

findで探し出したものをuser.idとuser.nameで連想配列を作る。

$this->set

  • 使い方

this->set('変数名', );

contorollerの変数はそのままではviewで使うことができないので一度


$item = 'apple';
$this->set('fruits', $item);

のようにsetに入れることでviewに変数を渡すことができるようになる


$this->set(compact("name", "address", "tel"));

このようにまとめることもできる。
viewでは

$name $address $tel

のように$をつけるだけで使える。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?