how to add naming conventions for entries in table twice? - cakephp

I've table called Uploads.
I've to make entries in this tables twice with 'type'
i.e. for youtube video link and image in table uploads
So I've written code like this
//for video link
echo $form->input('Project.Upload.Name', array('type'=>'file','label' => false));
and
//for image
echo $form->input('Project.Upload.Name', array('type'=>'file','label' => false));
How can I differentiate these two fields?
Is there any way so of naming convention so that I can separate out fields for links and images.

If you want two Upload-fields, you have to name them like this:
echo $form->input('Project.Upload.0.Name', array('type'=>'file','label' => false));
echo $form->input('Project.Upload.1.Name', array('type'=>'file','label' => false));
This will create an array in $this->data ready for use. Have a look on how to save related model data in the Cookbook for more and deeper information.
Edit
If you need to keep track of what type your upload is you have to add it like this:
echo $form->input('Project.Upload.0.Name', array('type'=>'file','label' => false));
echo $form->input('Project.Upload.0.Type', array('type'=>'hidden','value' => 'image'));
echo $form->input('Project.Upload.1.Name', array('type'=>'file','label' => false));
echo $form->input('Project.Upload.1.Type', array('type'=>'hidden','label' => 'video'));
What you do here is that you associate your first upload with the first type-field which has the hidden value 'image'. So the first entry would be stored in your database something like this (as Cake-Array):
The processing of the image and the video must of course be done before saving it so the database.
array(
[Upload] => array(
[0] => array(
[id] => 1,
[name] => 'test.jpg',
[type] => 'image'
),
[1] => array(
[id] => 1,
[name] => 'test.avi',
[type] => 'video'
)
)
)

Related

CakeDC Ratings stars do now appear and data is not saved

I am trying to add CakeDC ratings to my CakePHP website. I have followed the instructions in readme provided with the plugin, but the rating stars do not appear. Also the ratings are not saved when I press 'Rate'. Here is the code from the view file:
<h3><?php echo ($post['Post']['title']); ?></h3>
<?php echo $this->Rating->display(array(
'item' => $post['Post']['id'],
'type' => 'radio',
'stars' => 5,
'value' => $post['Post']['rating'],
'createForm' => array('url' => array_merge($this -> passedArgs, array('rate' => $post['Post']['id'], 'redirect' => true)))));?></h3>
After pressing "Rate", the url changes to '.../rate:2/redirect:1' , but the data does not appear in the database. Am I missing something?
i also encountered this problem, but what i did was, i added this
public $components = array('Ratings.Ratings');
public $actsAs = array('Ratings.Ratable');
and after that, it managed to save the data

HABTM association not saved - cakephp

I've been searching for all types of solutions and it's not working. I have two models, Bredbook and Genre, in bredbook.php and genre.php, associated with HABTM. No way to save the association.
Here is the code of one model:
public $hasAndBelongsToMany = array('Bredbook' => array('className' => 'Bredbook',
'joinTable' => 'bredbooks_genres',
'foreignKey' => 'genre_id',
'associationForeignKey' => 'bredbook_id'));
Here is the form:
echo $this->Form->create('Bredbook', array('action' => 'firstConnection'));
echo $this->Form->input('Genre', array('options' => $genres, 'empty' => false));
echo $this->Form->end('Validate');
And in BredbooksController.php:
if ($this->request->data)
{
if($this->Bredbook->saveAssociated($this->request->data))
return $this->redirect('/bredbooks/index');
}
The creation of the bredbook is ok, but no association is created in the table bredbooks_genres. I've tried everything... any help will be welcome.
You need to send Bredbook.id in your data array:
<?php
echo $this->Form->create('Bredbook');
//It can be any other attribute, but need some Breedbook data
echo $this->Form->input('Bredbook.id', array('value' => $id));
echo $this->Form->input(
'Genre',
array(
'options' =>$genres,
'empty' => false
)
);
echo $this->Form->end('Validate');
Or make sure your $this->request->data looks similar to this before saving:
Array
(
[Bredbook] => Array
(
//It can be any other attribute, but need some Breedbook data
[id] => 1
)
[Genre] => Array
(
[Genre] => Array
(
[0] => 1
)
)
)
This Solution will work also work when you are creating a new Bredbook as long as you send some data of that Model, it doesn't need to be the id, you can also send name or other attributes from your Bredbook Model. If you send Bredbook.id it will not create a new record and only make the associations, but if you don't send the id and send other attributes (like name for example) it will create a new record and will associate it with the Genre data your are sending.
I finally did it: I don't know if it's a necessary thing, but I just used cake bake to generate everything: http://book.cakephp.org/2.0/en/console-and-shells/code-generation-with-bake.html
Now I need some work to rearrange all the views, but my saving is working, with the exact same code I posted first, and I think I saved some time too with all that generated behaviour I needed !

CakePHP (2.1) Media Plugin - Multi File Upload

I'm using the cakephp media plugin in my project using the monolithic style attachments table, i.e. all the attachments go in the one table with foreign_key, model, group etc. saved with the file details. So my model looks like:
class ProjectProfile extends AppModel {
var $name = 'ProjectProfile';
var $useDbConfig = 'default';
var $useTable = 'project_profiles';
var $actsAs = array('Media.Transfer', 'Media.Generator');
public $belongsTo = array(
'Project' => array(
'className' => 'Project',
'foreignKey' => 'pjID'
)
);
var $hasMany = array(
'Photo' => array(
'className' => 'Media.Attachment',
'order' => 'Photo.basename, Photo.id',
'foreignKey' => 'foreign_key',
'conditions' => array('Photo.model' => 'ProjectProfile', 'Photo.group' => 'Photo'),
'dependent' => true)
);
Then a saveAll in the controller when saving my record saves the attached file.
This all works fine, however I'd really like to be able to upload multiple files at once, which the plugin does support by doing this in the form:
echo $this->Form->hidden('Photo.0.model', array('value' => 'Photo'));
echo $this->Form->input('Photo.0.file', array('type' => 'file');
echo $this->Form->hidden('Photo.1.model', array('value' => 'Photo'));
echo $this->Form->input('Photo.1.file', array('type' => 'file');
echo $this->Form->hidden('Photo.2.model', array('value' => 'Photo'));
echo $this->Form->input('Photo.2.file', array('type' => 'file');
But I think you'd agree that's a bit cumbersome to have to click browse for each individual file. The simplist method I could see to to allow multiple file uploads was to use the HTML5 multiple file section option - http://bakery.cakephp.org/articles/veganista/2012/01/31/html_5_multiple_file_upload_with_cake :
echo $this->Form->input('files.', array('type' => 'file', 'multiple'));
This allows you to shift click in the file browser to select multiple files then puts the files into an array to save... however, this field format isn't handled by the media plugin. Also, there'd be no way to add the model, group etc. fields on the save as far as I could see.
So, does anybody know how I can handle multi file uploads with the media plugin using the monolithic model? I'm open to all suggestions.
Thanks in advance.
Will this CakePHP 2.x plugin - AjaxMultiUpload - work for you? I think that does exactly what you need it to.

Refill relationships in form in cakephp in edit mode

I got a controller named Posts, a model called Content which is properly linked with other models such as Category and Location.
In my view for add 'Content' i successfully populate the multi select lists with categories and locations to pick to relate to the post. Saving it all works perfectly.
Now in edit/update mode, I can once again fill the multi selects with categories and locations, but it will not select the ones related to the current post. When looking in the database, there are categories and locations successfully realted to the current post.
This is what I got in my controller:
$this->data = $this->Content->read();
$this->set('locations',$this->Content->Location->find('list',array('fields' => array('id','location'))));
$this->set('categories',$this->Content->Category->find('list',array('fields' => array('id','category'))));
And this is what I got in my view:
echo $this->Form->input('Location', array('type' => 'select','multiple' => 'true','options' => $locations));
echo $this->Form->input('Category', array('type' => 'select','multiple' => 'true','options' => $categories));
What am i missing here? How do i get the already related locations and categories, select in the multi select lists?
(filling of non relationship data, will repopulate textfields etc just perfectly)
Grateful for any help!
Jason
instead of
$this->data = $this->Content->read()
try
$params['conditions'] = array(
'Content.id' => $id
);
$params['contain'] = array(
'Category',
'Location'
);
$this->data = $this->Content->find('first', $params);
You will need the Containable Behaviour for that
Use this:
echo $this->Form->input('Location', array(
'label' => 'Location',
'type' => 'select',
'options' => $LocationArray,
'selected'=> 12(Selected Value)
);

How do I write data to a HABTM association of a HABTM join table in CakePHP?

I'm trying to save data with following structure:
As you can see, there is HABTM association between users and experiences table. And another HABTM between experiences_users and tags. I created following form:
<?php echo $form->create('Experience', array('action' => 'addClassic'));?>
<?php
echo $form->input('Experience.date', array('dateFormat' => 'DMY'));
echo $form->input('Experience.time', array('timeFormat' => '24', 'empty' => array(-1 => '---'), 'default' => '-1'));
echo $form->input('Experience.name');
echo $form->input('ExperiencesUser.1.note');
echo $form->input('ExperiencesUser.1.rating');
//echo $form->input('Tags.Tags', array('multiple' => 'multiple', 'options' => $tags));
//echo $form->input('ExperiencesUser.1.Tags', array('multiple' => 'multiple', 'options' => $tags));
//echo $form->input('ExperiencesUser.1.Tags.Tags', array('multiple' => 'multiple', 'options' => $tags));
echo $form->input('ExperiencesUser.1.confirmed', array('type' => 'hidden', 'value' => '1'));
echo $form->input('ExperiencesUser.1.user_id', array('type' => 'hidden'));
echo $form->input('ExperiencesUser.2.user_id', array('type' => 'hidden'));
?>
<?php echo $form->end(__('Add', true));?>
And everything works well. saveAll method creates new Experience, assings this new experience to two users (via experiences_users) and sets the stuff around.
What bothers me is that I want to assign some Tags to newly created experiences_users record (to the first one). I thought, that should be done via some of the commented stuff. Every line of this commented code creates form and sends data to $this->data, but it never gets saved.
So my question is: What is the right syntax of $this->data or $form->input in my example to save data into experiences_users_tags?
I figured it out. Cake saveAll function works only for directly associated models. Luckily Cake is able to take care of this:
view
echo $form->input('Tags.Tags', array('multiple' => 'multiple', 'options' => $tags));
controller
$this->Experience->saveAll($data, $parameters);
$this->Experience->ExperiencesUser->save($data);
Cake after calling saveAll() fills $this->data with last inserted id. So second call save() will save data to the right table and set properly foreign keys.

Resources