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"
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.
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 }
Regardless of whether you have a mature website strategy or are just starting out, One of our qualified team members is happy to evaluate your website and current web marketing efforts one on one and give you our professional analysis.