Posts

test title

Long input 
created [2010-11-25 13:25:06]
modified [2010-11-25 13:25:06]




Store the same data in 2 seperate tables from 1 controller

I have a form that creates a new Dealer and also has a username and password field to designate to each dealer. I wanted these values to also be placed into the users table from the Dealers controller.

Keep in mind that I am in the Dealers controller and not the users controller.
I created a before function and called it before the save action of the add() function

I should also point out that I had to specify in the controller the $uses variable. This is usually dynamic but since I am working with an extra model for the controller, this is required.

var $uses = array('Store', 'User');

 

function add(){

if($this->beforeSave()){
if ($this->Store->save($this->data)) {
                      //session flash
               }
        }

}

 

function beforeSave(){

$userTblState = $this->data['Store']['state_id'];
       //keep state_id in variable to send it to the User table
$userTblName = $this->data['Store']['name'];
       //keep Store_name in variable to send it to the User table
$userTblUsername = $this->data['Store']['username'];
        //keep Store_username in variable to send it to the User table

  $data = array(
'User' => array(
'id' => '' ,
'name' => $userTblName,
'username' => $userTblUsername,
'password' => 'e4967li7b149767ytu1f42055d168799a73f514',
'state_id' => $userTblState,
)
);
        //Array of the data that needs to be set in the User table
        // leaving id in array blank let's it auto increment
$this->User->save($data);
        // If you notice the add() function up top is saving to a table named "Stores" and the beforeSave() is saving to a table named "User"
}

 
created [2010-11-20 17:41:15]
modified [2010-11-20 17:41:15]




Allow Users to view records that belong to a state that corresponds to the foreign key in their user table

I needed a way to restrict file access to users based on the state they live in. I have a table with user info and another table with a list of states. The users table pulls in a foreign key from the states table.

 
I needed an index view that would automatically restrict users to view only filepaths from the files table if their state corresponded to that file's state.

[controller: dealer_files_controller]
[app_controller: holding Auth Component]


function index(){
parent::beforeFilter();
     //accesses before filter in the app_controller
     $stateAllow = array('DealerFile.state_id' => $this->Auth->ser('state_id'));  
     //store the state_id of the user in the current session of the Auth Component
     $this->set('dealerFiles', $this->paginate("DealerFile", $stateAllow));
     //set only the state_id of the dealerFile if it equals the state_id of the  User and places it in the view
}

 
created [2010-11-20 17:15:32]
modified [2010-11-20 17:15:32]




Page 1 of 2

<< previous | 1 | 2 |