Yii Many to Many Relational Query -


Use a lot of relational queries to see the records of a particular client for a customer with many customers and customers. Try and redirect that to a different page if that customer is not affiliated with that user.

  // connection in client model public function relationship () {// Note: You may need to adjust the relation name and relationship to respective // ​​class name automatically below Is generated. Return Array ('Owner' = & gt; array (auto :: MANY_MANY, 'user', 'owner_client (owner_id, customer_id)'),); } // user model relation in relation to public relations () {return array ('clients' = & gt; array (auto :: MANY_MANY, 'customer', 'owner_client (owner_id, customer_id)'));); } // Define that the user can view this client / client client records $ client_record = Client :: model () - & gt; FindByPk ($ id); Users $ users = $ client_record- & gt; // To find owners, many queries; // If the user ID is not found in the array, then (if!! In_array (yii: app () -> User-> ID, $ user)) {$ this-> Redirect (Arrays ('/ Site / Dashboard')); }   

The above code redirection, even if I know that the client is related to the user

When you $ users = $ client_record-> Owners call , the one you are receiving back is an array of all your user models that are currently connected to the customer. As a result, you are comparing the integer for the objects, which means your in_re () condition will always fail.

I recommend that you create a conditional query to test your verification. Something like this should work:

  $ model = customer :: model () - & gt; With (array ('owner' = & gt; array ('select' = & gt; 'owner_id' 'condition' = & gt; 'user.id =' .Yii: app () -> user- & Gt; ID,))) - & gt; FindByPk ($ id); If ($ Model === Faucet) {$ this- & gt; Redirect (Arrays ('/ Site / Dashboard')); }    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -