0
votes

I am a bit confused about how I can exchange information between controllers in CakePHP 2.0. This is basically what I am trying to do.

I have images db table that holds id,name,path, etc. users table that holds user user, password, etc.

I have a model Image.php, a controller - ImagesController.php, and views - View/Images/add.ctp View/Images/index.ctp View/Images/view.ctp

I have a model User.php, a controller - UsersController.php, and views - View/Users/index.ctp, View/Users/login.ctp

My View/Images/index.ctp lists all the images that have been uploaded, View/Images/add.ctp lets anyone upload an image

View/Users/login.ctp lets the user log in and if it's correct info, it sends the user to View/Users/index.ctp

1) How can I make View/Images/add.ctp be accessible only to users who are logged in. Should I redirect them to View/Images/add.ctp with user session information and check if the data is set or how does it work in CakePHP?

2) If I am updating an images table from Images using $this->Image->saveAll($data_s). How can I save some of the data into User table? I have looked at the documentation but a bit confused still.

2

2 Answers

2
votes
  1. Use the AuthComponent to handle authentication, which is really what you want.
  2. If the two models are associated, you can access one through the other. E.g. if Image belongsTo User, the User model is accessible from the Image model via a simple $this->Image->User. Otherwise, you can include any model in any controller using the $uses attribute or temporarily using the loadModel method.
0
votes
  1. You should be doing this checking in the controller - if you're not logged in, don't let the code drop into a branch where it'll use that view. You shouldn't be putting that kind of logic into a view.

  2. Call $this->User->save($some_data) where $this->User has the User Model loaded into it, and $some_data is the narrowed subset of data you want to insert into the users table.