0

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

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

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;

1 Comment

I think this is a better way to make your code look cleaner, please accept if you use this answer...
0

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'); 

1 Comment

Nice, but.. I forgot to say that getValue has a parameter with the condition for a $this->find, you know? so, how could I show this array like $this->getValue('parameter')['Setting']['Value'];? thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.