cakephp - view with data from multiple tables - php

i am working on a Cakephp 2.x. I have two tables in my app: 'Images' and 'Audios'... i have a view in which i want to show the images and videos both sorted by date ... dont know how to do this ... because the main problem is in Cakephp if you want to show image we do this
echo $data['Image']['filename'];
and for audio i do this
echo $data['Audio']['filename'];
we have to specify the modal name specifically.. but now i dont want to do this.. i want to show in random sorted by date.. for example if today someone uploaded an image and yesterday an audio was uploaded ... so the data has to look like this on the view page
audio.mp3 ,image.jpg .....so on
like this ..
i dont want to write like this
foreach($datas as $data){
echo $data['Image']['filename'];
echo $data['Audio']['filename'];
}
here i am explicitly telling that image comes first and then the audio which i dont want ...
my Images Model
function getImagesAndAudio($userid){
$this->bindModel(array(
'belongsTo' => array(
'Audio' => array(
'className' => 'Audio',
'foreignKey' => false,
'conditions' => array(
'Image.user_id = Audio.user_id',
),
'type' => 'LEFT',
)
)
), false);
return $this->find('all', array('conditions' => array('Image.User_id' => $userid),
'contain' => array('Audio' ),
));
}
passing data from controller like this
$data = $this->Image->getImagesAndAudio($userid);
$this->set('datas', $data);

Related

Cake PHP 3.5 multiple pagination

I have a media library where i load images and videos, both should have a pagination. So far it works and i can load e.g. the page 2,3... with ajax and replace the content for each entity.
Now the problem is the first page + the first- and prev- buttons if they point to the first page from each entity. Why?
The created links look like :
working one : http://www.xyz.de/media?image[page]=2
not working : http://www.xyz.de/.media
This occurs in the image- and video-paginaton for the first page.
What I am doing wrong? I read the Paginator-Component and the Paginator-Helper Doku 100 times and can firure it out.
I have my MediaController where I set up my paginator as follows :
public $paginate = [
'Images' => ['limit' => 10, 'scope' => 'image'],
'Videos' => ['limit' => 8, 'scope' => 'video'],
'limit' => 12
];
than in my controller-action i set the values :
$images = $this->paginate($imageTable->find('all',$image_options), ['scope' => 'image']);
$videos = $this->paginate($videosTable->find('all', $video_options), ['scope' => 'video']);
$this->set(compact('images', 'videos'));
in my view I use the PaginatorHelper to set e.g. the numbers like so
$paginator->numbers(['model' => 'Images'])

Different current page in one controller

I am using pagination on my CakePHP page and it works well, but I have a few pages generated by different functions in one controller file and there is an cumbersome issue.
Let's assume I've got two tables with hundreds of entries, which my paginators divide and display 15 of entries on one page:
public $paginate = array(
'Table1' => Array(
'order' => array('Table1.id' => 'desc'),
'limit' => 15
),
'Table2' => Array(
'order' => array('Table2.id' => 'asc'),
'limit' => 15
)
);
And functions in this controller file:
public function showTable1() {
$this->set('table', $this->paginate('Table1'));
}
public function showTable2() {
$this->set('table', $this->paginate('Table2));
}
When I go to the next page while displaying "Table 1", I go to the next page and it works well. But when I open another page with "Table 2" generated by another function from the same controller file, it unfortunately also goes to the second page.
What should I do to have different "pages indicators"? I mean, when I go to the third page on "Table 1", I don't want to go to the third page when I go to "Table 2".
Thanks in advance. I hope I presented my problem clearly.
instead of setting the pagination options on the top of the controller, try deleting that public $paginate and setting all the options inside action, like this (and in the similar way for the second action)
public function showTable1() {
$options = array(
'conditions' => array(
'Table1.id' => whatever
),
'fields' => array(
'Table1.id'
),
'order' => array(
'Table1.id' => 'DESC'
),
'limit' => 15
);
$this->Paginator->settings = $options;
$table = $this->Paginator->paginate('Table1');
$this->set(compact('table'));
}

cakephp control dropdown population from database

I'm trying to populate a dropdown/select tag in cakephp,fortunately I was able to populate it with the values coming from my database,
however it displays ALL. How can I limit the population to a specific ID?
To make things clear here's what I want to achieve:
Apples:
form start
dropdown-contains only values that are associated with apples from the DB
button
form end
Currently here's how it looks like
Apples:
form start
dropdown-contains all fruits values from the DB
button
form end
Here's my current code tnx any help/suggestions is very much appreciated
Controller:
$fruits = $this->Model1->Model2->find('list',
array('fields' =>
array('id',
'fruit_name',
//'conditions' => array(''=>'')
)));
$this->set('fruitsList', $fruits);
View:
echo $this->Form->input('Model1.salad_fruits_id',
array('type' => 'select',
'options' => $fruitsList,
));
If you have association between models between models it would be easier. Just add conditions to second model.
//if you don't have association between models
$this->Model1->bindModel(array(
'belongsTo' => array( //example binding
'Model2' => array(
'className' => 'Model2',
'foreignKey' => 'model2_id',
)
)
));
$fruits = $this->Model1->find('list', array(
'conditions' => array(
'Model2.name' => 'apple'
),
'fields' => array(
'id','fruit_name',
)
));

Cakephp $this->request->data returns id of select field instead of value which is needed and shown in the select field

I have a CakePHP controller like this:
$this->loadModel('Project');
$list2 = $this->Project->find( 'list', array(
'fields' => array('Project.project'),
'conditions' => array('Project.user_id' => $userId)
));
$this->set($list2, 'list2');
$this->loadModel('Distance');
if(!empty($this->request->data)){
$this->Distance->create();
if ($this->Distance->save($this->request->data)) {
$this->Session->setFlash('Saved.');
// $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('FAILED');
}
}else{
// $this->Session->setFlash('test');
}
and a view like this:
echo $this->Form->input('Distance.project', array('options' => $list2, 'label' => false, 'empty' => '(choose one)' ;
But I get inserted to the database the id of the project instead of the project name.
I never have such problems working with the fields - just with a list of data.
Any idea why it happens?
It's normal ... The $list2 it's and array ... and the values of the options are the indexes from that array.
If you want to insert only the project name you need to change $list2 with $list2['project_name']. You need to remove or replace the indexes of $list2.
LE: take iexiak example. He change also the code for you.
$list2 = $this->Project->find( 'list', array(
'fields' => array('Project.project'),
'conditions' => array('Project.user_id' => $userId)
)
);
This is because $list2 automatically creates a list of ID => project; and when you use that as input for your form it automatically creates the drop down to reflect this. This is generally the best practice, to link to ID's instead of to descriptions, as ID's do not change as often. The below should get you exactly what you want though:
$list2 = $this->Project->find( 'list', array(
'fields' => array('Project.project','Project.project'),
'conditions' => array('Project.user_id' => $userId)
)
);

Exposing a table to Views

I have a table that's been created by a module. I need to include some of its fields into an existing view.
I tried using the table wizard module, but all it does is create a separate view for that table. I'd like to be able to choose fields from that table to be added into an existing view as additional fields, or through relationships or something like that. Is there a workaround to do what I'm trying to do?
Ah. Views. Took me a while as well. This answer is for Drupal 6 and in the abstract shows how to define fields as well as using a relationship to allow the fields to link to the node table.
Inside modulename.module, you want a function that goes:
function modulename_views_api() {
return array(
'api' => 2,
);
}
Then you want to make a file called modulename.views.inc and define a function like this:
function modulename_views_data() {
$data['modulename_table'] = array(
'table' => array(
'group' => 'ModuleName',
'title' => 'Module name title',
),
'join' => array(
// to join to node, we'll use a field in modulename_table called 'nid'
'node' => array(
'left_field' => 'nid',
'field' => 'nid',
),
),
);
// now we define the fields in the table like this
// check out modules/views/handlers to see more specific handlers
$data['modulename_table']['fieldname'] = array(
'title' => 'fieldname',
'help' => 'fieldname description',
'field' => array(
'handler' => 'views_handler_field',
),
);
$data['modulename_table']['nid'] = array(
'title' => 'related node',
'help' => 'the field that relates back to {node}',
// here we implement a relationship to nid
'relationship' => array(
'base' => 'node',
'field' => 'nid',
'handler' => 'views_handler_relationship',
'label' => 'modulename row node',
),
// this relationship can be turned on in views
);
return $data;
}
You can use hook_views_data to define your table in code. As long as you don't want views to do special manipulations, it's almost as simple as defining the table with the schema API.
Your other option is to use table wizard to expose the tables to the database and then use the migrate module to create the views. http://drupal.org/project/migrate
I have found that the Views Custom Field module lets me do just about anything I need as far as adding oddball fields to views .. maybe it'd help ..

Categories