{FR} - 3.5.3.4.4 postConditions
Utilisez cette méthode pour transformer des données de formulaire, transmises par POST (depuis les inputs du Helper Form), en des conditions de recherche pour un modèle. Cette fonction offre un raccourci appréciable pour la construction de la logique de recherche. Par exemple, un administrateur aimerait pouvoir chercher des commandes dans le but de connaître quels produits doivent être emballés. Vous pouvez utiliser les Helpers Form et Html pour construire un formulaire rapide basé sur le modèle Commande. Ensuite une action du contrôleur peut utiliser les données postées par ce formulaire pour construire automatiquement les conditions de la recherche :
function index() {
$o = $this->Commande->findAll($this->postConditions($this->data));
$this->set('commandes', $o);
}
function index() {$o = $this->Commande->findAll($this->postConditions($this->data));$this->set('commandes', $o);}
Si $this->data[‘Commande’][‘destination’] vaut "Boulangerie du village", postConditions convertit cette condition en un tableau compatible avec la méthode Model->findAll(). Soit dans notre cas, array("Commande.destination" => "Boulangerie du village").
Si vous voulez utiliser un opérateur SQL différent entre chaque terme, remplacez-le en utilisant le second paramètre :
/*
Contenu de $this->data
array(
'Commande' => array(
'nb_items' => '4',
'referrer' => 'Ye Olde'
)
)
*/
//Trouvons les commandes qui ont au moins 4 items et qui contiennent ‘Ye Olde’
$o = $this->Order->findAll($this->postConditions(
$this->data,
array('>=', 'LIKE')
));
/*Contenu de $this->dataarray('Commande' => array('nb_items' => '4','referrer' => 'Ye Olde'))*///Trouvons les commandes qui ont au moins 4 items et qui contiennent ‘Ye Olde’$o = $this->Order->findAll($this->postConditions($this->data,array('>=', 'LIKE')));
L'ordre dans lequel sont spécifiés les opérateurs est celui des colonnes dans le tableau $this->data. Etant donné que 'nb_item' est le premier, l'opérateur >= s'appliquera donc à ce champ.
Le troisième paramètre vous permet de dire à CakePHP quel opérateur booléen SQL utilisé entre les conditions de recherche. Les chaînes comme "AND", "OR" et "XOR" sont des valeurs possibles.
Enfin, si le dernier paramètre est défini à vrai et que $op est un tableau, les champs non-inclus dans $op ne seront pas inclus dans les conditions retournées.
{EN} - 3.5.3.4.4 postConditions
postConditions(array $data, mixed $op, string $bool, boolean $exclusive)
Use this method to turn a set of POSTed model data (from HtmlHelper-compatible inputs) into a set of find conditions for a model. This function offers a quick shortcut on building search logic. For example, an administrative user may want to be able to search orders in order to know which items need to be shipped. You can use CakePHP’s Form- and HtmlHelpers to create a quick form based on the Order model. Then a controller action can use the data posted from that form to craft find conditions:
function index() {
$o = $this->Orders->findAll($this->postConditions($this->data));
$this->set('orders', $o);
}
function index() {$o = $this->Orders->findAll($this->postConditions($this->data));$this->set('orders', $o);}
If $this->data[‘Order’][‘destination’] equals “Old Towne Bakery”, postConditions converts that condition to an array compatible for use in a Model->findAll() method. In this case, array(“Order.destination” => “Old Towne Bakery”).
If you want use a different SQL operator between terms, supply them using the second parameter.
/*
Contents of $this->data
array(
'Order' => array(
'num_items' => '4',
'referrer' => 'Ye Olde'
)
)
*/
//Let’s get orders that have at least 4 items and contain ‘Ye Olde’
$o = $this->Order->findAll($this->postConditions(
$this->data,
array('>=', 'LIKE')
));
/*Contents of $this->dataarray('Order' => array('num_items' => '4','referrer' => 'Ye Olde'))*///Let’s get orders that have at least 4 items and contain ‘Ye Olde’$o = $this->Order->findAll($this->postConditions($this->data,array('>=', 'LIKE')));
The key in specifying the operators is the order of the columns in the $this->data array. Since num_items is first, the >= operator applies to it.
The third parameter allows you to tell CakePHP what SQL boolean operator to use between the find conditions. String like ‘AND’, ‘OR’ and ‘XOR’ are all valid values.
Finally, if the last parameter is set to true, and the $op parameter is an array, fields not included in $op will not be included in the returned conditions.
Differences
| Lines: 1-31 | Lines: 1-37 | ||
| <title>postConditions</title> | <title>postConditions</title> | ||
| - | <p class="method"><code>postConditions(array $data, mixed $op, string $bool, boolean $exclusive)</code></p>r /><p>Use this method to turn a set of POSTed model data (from HtmlHelper-compatible inputs) into a set of find conditions for a model. This function offers a quick shortcut on building search logic. For example, an administrative user may want to be able to search orders in order to know which items need to be shipped. You can use CakePHP’s Form- and HtmlHelpers to create a quick form based on the Order model. Then a controller action can use the data posted from that form to craft find conditions:</p> | + | <p> Utilisez cette méthode pour transformer des données de formulaire, transmises par POST (depuis les inputs du Helper Form), en des conditions de recherche pour un modèle. Cette fonction offre un raccourci appréciable pour la construction de la logique de recherche. Par exemple, un administrateur aimerait pouvoir chercher des commandes dans le but de connaître quels produits doivent être emballés. Vous pouvez utiliser les Helpers Form et Html pour construire un formulaire rapide basé sur le modèle Commande. Ensuite une action du contrôleur peut utiliser les données postées par ce formulaire pour construire automatiquement les conditions de la recherche : </p> |
| <pre> | <pre> | ||
| function index() { | function index() { | ||
| - | $o = $this->Orders->findAll($this->postConditions($this->data)); $this->set('orders', $o); |
+ | $o = $this->Commande->findAll($this->postConditions($this->data)); $this->set('commandes', $o); |
| } | } | ||
| </pre> | </pre> | ||
| - | <p>If $this->data[‘Order’][‘destination’] equals “Old Towne Bakery”, postConditions converts that condition to an array compatible for use in a Model->findAll() method. In this case, array(“Order.destination” => “Old Towne Bakery”).</p> <p>If you want use a different SQL operator between terms, supply them using the second parameter.</p> |
+ | <p>Si <code>$this->data[‘Commande’][‘destination’]</code> vaut "Boulangerie du village", postConditions convertit cette condition en un tableau compatible avec la méthode Model->findAll(). Soit dans notre cas, array("Commande.destination" => "Boulangerie du village"). </p> <p>Si vous voulez utiliser un opérateur SQL différent entre chaque terme, remplacez-le en utilisant le second paramètre :r /></p> |
| <pre> | <pre> | ||
| /* | /* | ||
| - | Contents of $this->data | + | Contenu de $this->data |
| array( | array( | ||
| - | 'Order' => array( 'num_items' => '4', |
+ | 'Commande' => array( 'nb_items' => '4', |
| 'referrer' => 'Ye Olde' | 'referrer' => 'Ye Olde' | ||
| ) | ) | ||
| ) | ) | ||
| */ | */ | ||
| - | //Let’s get orders that have at least 4 items and contain ‘Ye Olde’ | + | //Trouvons les commandes qui ont au moins 4 items et qui contiennent ‘Ye Olde’ |
| $o = $this->Order->findAll($this->postConditions( | $o = $this->Order->findAll($this->postConditions( | ||
| $this->data, | $this->data, | ||
| array('>=', 'LIKE') | array('>=', 'LIKE') | ||
| )); | )); | ||
| </pre> | </pre> | ||
| - | <p>The key in specifying the operators is the order of the columns in the $this->data array. Since num_items is first, the >= operator applies to it.</p> <p>The third parameter allows you to tell CakePHP what SQL boolean operator to use between the find conditions. String like ‘AND’, ‘OR’ and ‘XOR’ are all valid values.</p> <p>Finally, if the last parameter is set to true, and the $op parameter is an array, fields not included in $op will not be included in the returned conditions.</p> |
+ | <p>L'ordre dans lequel sont spécifiés les opérateurs est celui des colonnes dans le tableau $this->data. Etant donné que 'nb_item' est le premier, l'opérateur >= s'appliquera donc à ce champ. </p> <p>Le troisième paramètre vous permet de dire à CakePHP quel opérateur booléen SQL utilisé entre les conditions de recherche. Les chaînes comme "AND", "OR" et "XOR" sont des valeurs possibles. </p> <p>Enfin, si le dernier paramètre est défini à vrai et que $op est un tableau, les champs non-inclus dans $op ne seront pas inclus dans les conditions retournées. </p> |
