Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For a 'company' prefix in Cakephp 2.1 - public $tablePrefix in the model and setting the tablePrefix in the controller, here is what I am doing and it is too many lines of code (one line of code per table that has a prefix per user) but it is all I can do right now.</p> <p>Modela.php:</p> <pre><code>public $tablePrefix = 'anything_'; </code></pre> <p>Modelb.php:</p> <pre><code>public $tablePrefix = 'anything_'; </code></pre> <p>Modelc.php:</p> <pre><code>public $tablePrefix = 'anything_'; </code></pre> <p>then in the public function beforeFilter() of the AppController</p> <pre><code>if (isset($this-&gt;Modela-&gt;tablePrefix)) {$this-&gt;Modela-&gt;tablePrefix = $this-&gt;Auth-&gt;user('company_code').'_'; } if (isset($this-&gt;Modelb-&gt;tablePrefix)) {$this-&gt;Modelb-&gt;tablePrefix = $this-&gt;Auth-&gt;user('company_code').'_'; } if (isset($this-&gt;Modelc-&gt;tablePrefix)) {$this-&gt;Modelc-&gt;tablePrefix = $this-&gt;Auth-&gt;user('company_code').'_'; } </code></pre> <p>I don't get the 'overloaded property' error this way, but in my schema, all my users have a valid company code. I also have for example the 'users' table is shared for all users (although only admins can get to a user record other than the logged in user)</p> <p>However, I find this rather cumbersome, and am interested in a means that would not require every single model that is to be segregated by the model's $tablePrefix company_code setting to be defined as a line of code in the AppController. The point at which I really want to set the tablePrefix for a model is the instant I reference </p> <pre><code>$this-&gt;loadModel("Modelb"); </code></pre> <p>But I spent 45 minutes playing with modelb.php in the Model directory and was not able to do much with the $tablePrefix property. When a model is instantiated or referenced, I should know, for those tables that need a $tablePrefix property, the required prefix, but I was not able to set the property dynamically at that point. (a dynamic static property? sounds like the wrong approach)</p> <p>So, even though this works, I don't like it. It feels like I am using Fortran methods in a Lisp program.</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. This table or related slice is empty.
    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