Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>yes it does. I have this in my app model to make transactions easy.</p> <p><a href="https://github.com/infinitas/infinitas/blob/dev/Model/AppModel.php#L677" rel="nofollow noreferrer">https://github.com/infinitas/infinitas/blob/dev/Model/AppModel.php#L677</a></p> <pre><code> /** * @brief wrapper for transactions * * Allow you to easily call transactions manually if you need to do saving * of lots of data, or just nested relations etc. * * @code * // start a transaction * $this-&gt;transaction(); * * // rollback if things are wrong (undo) * $this-&gt;transaction(false); * * // commit the sql if all is good * $this-&gt;transaction(true); * @endcode * * @access public * * @param mixed $action what the command should do * * @return see the methods for tranasactions in cakephp dbo */ public function transaction($action = null) { $this-&gt;__dataSource = $this-&gt;getDataSource(); $return = false; if($action === null) { $return = $this-&gt;__dataSource-&gt;begin($this); } else if($action === true) { $return = $this-&gt;__dataSource-&gt;commit($this); } else if($action === false) { $return = $this-&gt;__dataSource-&gt;rollback($this); } return $return; } </code></pre> <p>then you can do something like this:</p> <pre><code>$saved = true; $this-&gt;transaction(); $saved = $saved &amp;&amp; $this-&gt;save($data); $saved = $saved &amp;&amp; $this-&gt;SomeOtherModel-&gt;save($data2); $saved = $saved &amp;&amp; $this-&gt;AnotherModel-&gt;save($data3); if($saved){ $this-&gt;transaction(true); return $this-&gt;id; } $this-&gt;transaction(false); return false; </code></pre> <p>you can also do more complex things like the following:</p> <pre><code>function save1(){ $saved = true; $this-&gt;transaction(); $saved = $saved &amp;&amp; $this-&gt;save($data); $saved = $saved &amp;&amp; $this-&gt;save2($data); if($saved){ $this-&gt;transaction(true); return $this-&gt;id; } $this-&gt;transaction(false); return false; } </code></pre> <p>cake does not support nested transactions, but you can sort of fake them</p> <p><code>// this will use transactions if its called directly, but will allow a calling method to</code></p> <p><code>// create and manage the transaction.</code></p> <pre><code>function save2($data){ $saved = true; $transaction = $this-&gt;transaction(); // will only be true if not already started $saved = $saved &amp;&amp; $this-&gt;save($data); if($transaction){ // if it was started here, finish it if($saved){ $this-&gt;transaction(true); return true; } $this-&gt;transaction(false); return false; } return $saved; // return just the status so the other model will finish the transaction } </code></pre> <p>just to be clear, you could be doing something like ClassRegistry::init('SomeRandomModel')->save2(). the transactions are not limited to the current model, or related models. its for any model.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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