Take data from a specific cakephp field

Asked

Viewed 1,137 times

0

Etou starting now with Cakephp, and I’d like to know how to get the value of a specific field.

For example, I have the name and email fields. How to get the name field value?

PS: I know you have the method $this->data, but he takes all the fields.

My form looks like this:

echo $this->Form->create('Usuario', array('action' => 'get'));
echo $this->Form->input('usuario');
echo $this->Form->input('email');
echo $this->Form->input('senha', array('type' => 'password'));
echo $this->Form->input('ativo');
echo $this->Form->end('Salvar');
  • 1

    Do you refer to the fields of a form? If it is, it is possible to show us how your form is?

  • 1

    Or, if it’s not what Paulo Rodrigues said, he wants to get the data from Model?

  • Exact @Paulorodrigues. No matter what field I want to pick up. I just want to know how to pick up the value of a field individually.

1 answer

1


It is not clear in which situation you want to take this data. If it is the value of the field after sending the form, this is usually done in the Controller:

$usuario = $this->request->data['NomeDoModel']['usuario'];

If you are from the database, you ask for the model (also from the Controller):

$usuario = $this->NomeDoModel->find('all', array(
    'fields' => 'usuario',
    'conditions' => "id = 1" // por exemplo
));
echo $usuario[0]['NomeDoModel']['usuario'];

In the view itself, if the data has been set in the data from the Controler, you can access directly via:

$this->data['NomeDoModel']['usuario'];

Or pass any variable. For example, inside your controller, action index:

function index() {
    this->set(array('dado' => 'bla bla bla'));
}

// e na view:
echo $dado;
  • I managed, using the name of my controller following the name of the field.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.