Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all:</p> <blockquote> <p>Let's say the products I am storing come from a supplier who provide me with a unique product id which I want to use for my primary key in the database.</p> </blockquote> <p>That is a SKU number. Keep your primary keys for the database's management. If there is an external identifier, store that separately.</p> <p>Then I would always reuse the form for both insert and update. This becomes quite trivial when your SKU is a different property than your primary key. An example in code, see this model:</p> <pre><code>&lt;?php class Product { protected $id; protected $sku; protected $name; // getters &amp; setters here } class Form extends \Zend\Form\Form { public function __construct() { $this-&gt;add(array( 'name' =&gt; 'sku', )); $this-&gt;add(array( 'name' =&gt; 'name', )); } } class Controller extends \Zend\Mvc\Controller\AbstractActionController { public function createAction() { $form = new Form; $product = new Product; $form-&gt;bind($product); if ($this-&gt;getRequest()-&gt;isPost()) { $data = $this-&gt;getRequest()-&gt;getPost(); $form-&gt;setData($data); if ($form-&gt;isValid()) { // get service to update $service-&gt;create($product); // redirect to view } } return new ViewModel(array( 'form' =&gt; $form, )); } public function updateAction() { $form = new Form; // Load product based on the ID $form-&gt;bind($product); if ($this-&gt;getRequest()-&gt;isPost()) { $data = $this-&gt;getRequest()-&gt;getPost(); $form-&gt;setData($data); if ($form-&gt;isValid()) { // get service to update $service-&gt;update($product); // redirect to view } } return new ViewModel(array( 'form' =&gt; $form, 'product' =&gt; $product, )); } } </code></pre> <p>This means the model keeps always an ID "for itself" and the SKU can be updated anytime. The update uses the id from the request (e.g. you have an url <code>admin/product/edit/123</code>) and the SKU number is differently and can be updated.</p> <p>By this means, the form can be kept the same for both the update and the create methods. I usually use a service layer to persist the object, you should do it the way you prefer.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload