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.
Related
Hello there,
thanks in adv for help.
i have two table: named as options fields are (id,option_title,created,status) and second table as option values fields as (id,option value,options_id,created,status). THe option values table has a foreign key as options_id. Now when i created association between these two table . i didn't work.. Let me explain you how i am creating association.
In the Option model i am using following code.
var $hasMany = array(
'values' => array(
'className' => 'Optionvalue',
'foreignKey' => 'options_id'
));
But this is not working and i am not able to get data from options table. Can any please advice me.. where i am wrong. I just need options table data in the optionsvalue add view file.
Assuming you have a table called options and another called optionvalues. Here is how the Option model looks like
class Option extends AppModel {
public $hasMany = array(
'Optionvalue' => array(
'className' => 'Optionvalue',
'foreignKey' => 'option_id',
'dependent' => FALSE
)
);
}
In the Optionvalue model you then go like this
class Optionvalue extends AppModel {
public $belongsTo = array(
'Option' => array(
'className' => 'Option',
'foreignKey' => 'option_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
This is how it should look like. In the Optionvalue Add method, you will need to write this in the view
echo $this->Form->create('Optionvalue');
echo $this->Form->input('option_id');
echo $this->Form->end('Save');
and the options will appear by themselves.
Very important though, you did not follow the naming conventions. options_id needs to be option_id among others.
Use
option_id in place of 'options_id'
Cakephp strongly follows naming conventions.
http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html
Also looks like you are not using proper naming conventions for other places too.
I try to code a order system and it is my first cake project.
i have a order, a user and a useraddress.
//Order model
public $belongsTo = array(
'User' => array(
'className' => 'User'
));
//User model
public $hasOne = array(
'Useradr' => array(
'className' => 'Useradr',
'foreignKey' => 'user_id',
'order' => ''
)
);
//maybe not necessary Useradr model
public $belongsTo = 'User';
Now i want to access the useraddress while i add/edit a voucher.
I can access the User in the voucher edit view:
echo $this->Form->input('User.email',array(
'label' => 'Email',
));
But not the Useraddress. What iam doing wrong ?
Thank you very much!
Julius
You have to use the correct model prefix.
echo $this->Form->input('Useradr.some_field',array(
'label' => 'Some Field',
));
I suggest you to do the blog tutorial instead of starting blindly coding. Also reading about the conventions is a good idea.
Further Useradr is a pretty bad name, better would be UserAddress. Abbreviations suck for multiple reason. First they're hard to guess if you don't know what they mean and the inflector very likey does not like them either and might screw up the singular/plural cases. Read Clean Code, very nice book about how to write clean code.
I am trying to work with HABTM association between Profiles and Qualifications tables.
Model: Profile.php
App::uses('AppModel', 'Model');
class Profile extends AppModel {
public $hasAndBelongsToMany = array(
'Qualification' => array(
'className' => 'Qualification',
'joinTable' => 'profile_qualifications',
'foreignKey' => 'profile_id',
'associationForeignKey' => 'qualification_id',
'unique' => 'keepExisting'
)
);
}
Model: Qualification.php
App::uses('AppModel', 'Model');
class Qualification extends AppModel {
public $hasAndBelongsToMany = array(
'Profile' => array(
'className' => 'Profile',
'joinTable' => 'profile_qualifications',
'foreignKey' => 'qualification_id',
'associationForeignKey' => 'profile_id',
'unique' => 'keepExisting',
)
);
}
Controller: ProfilesController.php
App::uses('AppController', 'Controller');
class ProfilesController extends AppController {
public function edit() {
$this->Profile->id = $this->Auth->user('profile_id');
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Profile->save($this->request->data)) {
$this->Session->setFlash(__('The profile has been saved'));
$this->redirect(array('action' => 'view'));
} else {
$this->Session->setFlash(__('The profile could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->Profile->read(null, $this->Auth->user('profile_id'));
}
$salutations = $this->Profile->Salutation->find('list', array('fields' => array('Salutation.id', 'Salutation.abbr_name')));
$qualifications = $this->Profile->Qualification->find('list', array('fields' => array('Qualification.id', 'Qualification.abbr_name')));
$this->set(compact('salutations', 'qualifications'));
}
}
Vew: edit.ctp
<div class="profiles form">
<?php echo $this->Form->create('Profile'); ?>
<fieldset>
<legend><?php echo __('My Profile'); ?></legend>
<?php
echo $this->Form->input('salutation_id');
echo $this->Form->input('first_name');
echo $this->Form->input('middle_name');
echo $this->Form->input('last_name');
echo $this->Form->input('qualification'); /* gives drop down not multi select */
echo $this->Form->input('bio');
echo $this->Form->input('email');
echo $this->Form->input('mobile');
echo $this->Form->input('phone');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
The edit view thus generated contains drop down to select a single value at a time for Qualifications attribute.
I want to know how can I generate a view with multi value selection box for qualifications ?
Moreover, what is the mistake in my code right now ?
First time poster, long time user.
I stumbled across this question today, and ended up using the subsequent solution which indeed does work quite nicely. However, I left myself wondering "why wouldn't CakePHP pickup on the HABTM relationship properly?" Especially considering (at least in my case) that the majority of the files had been baked in the cake console.
If we dive a little deeper into the issue, we discover that the problem is actually quite simple. A closer look at this snippet in the PostsController.php reveals how Cake builds in the HABTM relationship to the function, and uses $this->set() in order to pass it to the view (IMPORTANT: using lower-case plural versions "salutations"):
$salutations = $this->Profile->Salutation->find('list', array('fields' => array('Salutation.id', 'Salutation.abbr_name')));
$qualifications = $this->Profile->Qualification->find('list', array('fields' => array('Qualification.id', 'Qualification.abbr_name')));
$this->set(compact('salutations', 'qualifications'));
According to the Cake Cook Book, in order to take advantage of this HABTM in the front end when using the form helper is to specify the variable in singular form & title case (ie: "Salutation")
Snippet from the cook book:
Assuming that User hasAndBelongsToMany Group. In your controller, set a camelCase plural variable (group -> groups in this case, or ExtraFunkyModel -> extraFunkyModels) with the select options. In the controller action you would put the following:
$this->set('groups', $this->User->Group->find('list'));
And in the view a multiple select can be created with this simple code:
echo $this->Form->input('Group');
Should solve your issue without any necessary field tweaking.
Cheers!
Bake on.
Perhaps you need further configuration of your input:
echo $this->Form->input('Qualification',array(
'label' => 'Qualifications',
'type' => 'select',
'multiple' => true, // or 'checkbox' if you want a set of checkboxes
'options' => $qualifications,
'selected' => $html->value('Qualification.Qualification'),
));
I've used this blog post whenever I've come up against HABTM associations. It seems to me that a set of checkboxes maybe desired by default over a select input - maybe someone with greater CakePHP insight can chime in here?
Change
echo $this->Form->input('qualification');
to
echo $this->Form->input('qualification', array(
'multiple' => true
));
The CakePHP manual has more information on the form helper input options.
I've created a simple CMS to manage several small websites built on CakePHP. After successfully migrating the plugin from 1.3 to 2.0 I'm running into a problem with the AuthComponent after updating Cake to the latest version 2.1.2.
The classnames of the plugin are all prefixed by the plugin's name to avoid duplicate classnames, as the application shares most of its tables with the plugin. So there are cases when I have a UsersController for the main application and a PluginNameUsersController for my CMS plugin (or a PostsController and a PluginNamePostsController). The plugin models rely on $useTable to find the correct database table (so PluginNamePostsController still uses posts).
Since upgrading to the 2.1.* branch of Cake the AuthComponent has ceased to work as it expects a non-existent pluginname_users table instead of referring to users.
Is it possible to define a custom table for the AuthComponent or is there any other method to get this working? Also, is this behaviour expected?
The component is configured in PluginNameAppController as follows:
public $components = array(
'Session', 'RequestHandler',
'Auth'=> array(
'loginAction' => array(
'controller' => 'pluginname_users',
'action' => 'login',
'plugin' => 'pluginname'
),
'loginRedirect' => array(
'controller' => 'pluginname_posts',
'action' => 'index',
'plugin' => 'pluginname'
),
'authenticate' => array(
'Form' => array(
'userModel' => 'PluginNameUser',
'fields' => array('username', 'password')
)
)
)
);
The AuthComponent is actually unaware of tables and such. By using a custom model, you can define $table on it to work on a different table. So, the first thing is to set up your plugin's model to use the proper table:
class PluginNameUser extends PluginAppModel {
public $table = 'users';
}
Then, tell your authentication methods to use the plugin's model by using the dot syntax.
'authenticate' => array(
'Form' => array(
'userModel' => 'PluginName.PluginNameUser',
'fields' => array('username', 'password')
)
)
Now when FormAuthenticate tries to authenticate, it will try and find the user using that model, which it now knows is in PluginName. It loads the model, which you've set to use the users table, and looks for the user there.
I've been trying, but in vain, to create multiple fixtures in cakePhP. Currently, I need to test a function that requires two different fixture database tables. If possible, could someone please post some simple snippet and show me create multiple fixtures.
Furnace,
have you taken a look at cake's bake utility? You can bake fixtures with it.
cake bake fixture all
bakes all fixtures at once in your app. Use with care in order not to overwrite existing code.
Edit0:
Use
cake bake fixture help
if you have further questions.
Here's a simple fixture: app/tests/fixtures/entity_fixtures.php
In your case you will create two of these files, one per model and with different names.
<?php
class EntityFixture extends CakeTestFixture {
var $name = 'Entity';
var $table = 'entities';
// Define the fields
var $fields = array(
'entity_id' => array('type' => 'integer', 'key' => 'primary'),
'type' => array('type' => 'string' , 'length' => 255),
'created' => 'datetime',
'modified' => 'datetime'
);
var $records = array(
array('entity_id' => 1, 'type' => 'entity', 'created'=>'2010-01-01', 'modified' => '2010-01-01')
);
}
?>
When in your test case include your fixtures at the top of your test case
class EntityTestCase extends CakeTestCase {
var $fixtures = array(
'plugin.myplugin.entity', // Including a sample plugin fixture
'entity', // The fixture above shown in code above
'blogs' // Including a third fixture (should be in app/tests/fixtures/blog_fixture.php)
);
}