문제

I don't want to declare a variable just for something so small.. check it out

$value = $this->getValue(); 
echo $value['Setting']['value'];

/\ $this->getValue() returns a array..

There is a way to use like this: echo $this->getValue()['Setting']['value']; ? thanks

도움이 되었습니까?

해결책 2

On your getValue(); function, make sure you return $value['Setting']['value'], so that on your calling function, you can just do echo $value;

For instance, I have this in my controller:

public function myFunction(){

     ...
     $value = $this->MyModel->getName($id);
     $this->set('value', $value);
}

...and I have this in my model to get the name value:

public function getName($id){

     $value = $this->find('first', array(
          'conditions' => array(
               'id' => $id 
          )
     ));

     return $value['MyModel']['name'];
}

...and so in your view, you can do:

echo $value;

다른 팁

You can do that in PHP 5.4. It is called Function Array Dereferencing. If you use <5.4 then you are out of luck.

This is not really an important problem, though. It may be slightly annoying but it is not the end of the world.

Look at how pretty much all the classes in the cake core do it. Configure::read(), CakeSession::read(), $this->request->query(), $this->request->data() etc. They all are able to return the value via dot syntax. So you could that here, as well.

So in the end you could just say:

echo $this->getValue('Setting.value'); 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top