Search code examples
functioncakephpcontrollermodels

cakePHP Calling functions between models/controllers


I am a relative newbie, so this may be blindingly obvious to most of you, but not to me!

I have 3 models (for this problem)

Project, ProjectDataset, and UserSessionDownload

A view in Project calls controller UserSessionDownloads to make a record of the download in a log table (working well thanks to help from Steve Moseley here) and then does the download. To download the file, the full pathname for the file is required.

So the UserSessionDownload function then calls the Model/Controller for ProjectDataset to get the filename for the chosen ProjectDataset (identified by id).

This filename will be then used to get the file in question.

Code parts are as follows: In Project view:

echo $this->Html->Link('Add a Ref', array('controller'=>'UserSessionDownloads', 'action'=>'add_download',$project_dataset['id'],$project['Project']['id']));

In UserSessionDownloads the function "add_download" then calls a function in ProjectDataset thusly (I have loaded ProjectDataset model into this function):

$filename = $this->ProjectDataset->get_filename($this->request->params['pass'][0]);

(the params refers to id of dataset passed in by call from Project view)

Now, I have tried the get_filename function being in Model and Controller files - but both seem to fail to get the required dataset data from the project_datasets table.

I have tried numerous combintations - but obviously not the right one.

For example, if I try putting function in Controller:

$this->set('projectDataset', $this->ProjectDataset->read(null, $id));
$filename = $this->projectDataset['dataset_filename'];

fails - saying the SQL is incorrect (does not even look like sql!)

and this:

$this->set('project_dataset', = $this->ProjectDataset->find('first',array('conditions' => array('id' => $id)));
        $filename = $this->project_dataset['dataset_filename'];

Fails reporting a SQL error.

While if I put function in Model, then I get errors saying the object does not exist..

Sorry for length of this, but would appreciate either suggested changes, or if you know something works, please do give me an example.

Thanks


Solution

  • Not sure what you are attempting with this

    $this->set('projectDataset', $this->ProjectDataset->read(null, $id));
    $filename = $this->projectDataset['dataset_filename'];
    

    But $this->set() in controller action sets a variable for use in the view, not for further use in the controller. (Unless you mean the $filename is in the view)

    Second, you wouldn't access the returned values of $this->ProjectDataset->read(null, $id) using $this->projectDataset unless you have assigned it previously.

    Grasping at straws from your question but I think possible solutions could be

    In controller

    $projectData = $this->ProjectDataset->findById($id);
    $filename = $projectData['ProjectDataSet']['dataset_filename'];
    

    In model

    class ProjectDataset extends AppModel
    {
        public function filename($id) 
        {
            $data = $this->findById($id);
            return $data['ProjectDataSet']['dataset_filename'];
        }
    }
    

    then call the following in you controller

    $filename = $this->ProjectDataset->filename($id);