= $product->id ?> |
= $product->getProductTypes()->name ?> |
= $product->name ?> |
= $product->price ?> |
= $product->active ?> |
= Tag::linkTo("products/edit/" . $product->id, 'Edit') ?> |
= Tag::linkTo("products/delete/" . $product->id, 'Delete') ?> |
Creating and Updating Records
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Now let's see how the CRUD creates and updates records. From the "new" and "edit" views the data entered by the user
are sent to the actions "create" and "save" that perform actions of "creating" and "updating" products respectively.
In the creation case, we recover the data submitted and assign them to a new "products" instance:
.. code-block:: php
id = $this->request->getPost("id", "int");
$products->product_types_id = $this->request->getPost("product_types_id", "int");
$products->name = $this->request->getPost("name", "striptags");
$products->price = $this->request->getPost("price", "double");
$products->active = $this->request->getPost("active");
//...
}
Data is filtered before being assigned to the object. This filtering is optional, the ORM escapes the input data and
performs additional casting according to the column types.
When saving we'll know whether the data conforms to the business rules and validations implemented in the model Products:
.. code-block:: php
create()) {
//The store failed, the following messages were produced
foreach ($products->getMessages() as $message) {
$this->flash->error((string) $message);
}
return $this->forward("products/new");
} else {
$this->flash->success("Product was created successfully");
return $this->forward("products/index");
}
}
Now, in the case of product updating, first we must present to the user the data that is currently in the edited record:
.. code-block:: php
id);
Tag::setDefault("product_types_id", $product->product_types_id);
Tag::setDefault("name", $product->name);
Tag::setDefault("price", $product->price);
Tag::setDefault("active", $product->active);
}
The "setDefault" helper sets a default value in the form on the attribute with the same name. Thanks to this,
the user can change any value and then sent it back to the database through to the "save" action:
.. code-block:: php
request->getPost("id"));
if (!$product) {
$this->flash->error("products does not exist " . $id);
return $this->forward("products/index");
}
//... assign the values to the object and store it
}
Changing the Title Dynamically
------------------------------
When you browse between one option and another will see that the title changes dynamically indicating where
we are currently working. This is achieved in each controller initializer:
.. code-block:: php
Conclusion
----------
This tutorial covers many more aspects of building applications with Phalcon, hope you have served to
learn more and get more out of the framework.
.. _Github: https://github.com/phalcon/invo
.. _CRUD: http://en.wikipedia.org/wiki/Create,_read,_update_and_delete
.. _Twitter Bootstrap: http://bootstrap.github.com/
.. _sha1: http://php.net/manual/en/function.sha1.php
.. _bcrypt: http://stackoverflow.com/questions/4795385/how-do-you-use-bcrypt-for-hashing-passwords-in-php