Upload Plugin issues - cakephp

Trying to get the CakePHP Upload Plug-in to work. The file uploads fine, the thumbnails are created...etc, but having a few issues:
-the 'name' field in the 'uploads' table is empty
-the 'upload' field in the 'uploads' table is empty ('attachment' field in the doc example)
-if I use {model} in the 'path' set in the Upload model, it uses 'upload' as the model folder - it should go in a folder of the associated model, not the upload model every time
//Upload MODEL
public $actsAs = array(
'Upload.Upload' => array(
'photo' => array(
'thumbnailSizes' => array(
'xvga' => '1024x768',
'vga' => '640x480',
'thumb' => '80x80',
),
'thumbnailMethod' => 'php',
'path' => '{ROOT}webroot{DS}uploads{DS}{model}{DS}{field}{DS}',
'maxSize' => '5242880', //5MB
'mimetypes' => array('image/jpeg', 'image/png', 'image/gif', 'image/bmp'),
'extensions' => array('jpg', 'gif', 'png'),
),
)
);
//ArticleData MODEL
public $hasMany = array(
'Upload' => array(
'className' => 'Upload',
'foreignKey' => 'foreign_key',
'conditions' => array(
'Upload.model' => 'ArticleData',
),
),
);
//CONTROLLER
public function admin_upload() {
if(!empty($this->request->data)) {
$this->loadModel('Upload');
debug($this->request->data);
if($this->Upload->save($this->request->data)) {
$this->Session->setFlash('SAVED!!!!!!!!');
} else {
$this->Session->setFlash('NOT SAVED!!!!!!!!');
}
}
}
// VIEW
echo $this->Form->create('ArticleData', array('type'=>'file'));
echo $this->Form->input('Upload.model', array('type'=>'hidden', 'value'=>'ArticleData'));
echo $this->Form->input('Upload.foreign_key', array('type'=>'hidden', 'value'=>'4f93676e-347c-4e0c-8e6c-0a3cadcd7f7c'));
echo $this->Form->input('Upload.photo', array('type'=>'file'));
echo $this->Form->end('Submit');

Dave:
-the 'name' field in the 'uploads' table is empty
This is normal, I think is is more of a "display" name field as opposed to "filename" field. The name should be stored in the "photo" field in your example.
-if I use {model} in the 'path' set in the Upload model, it uses 'upload' as the model folder - it should go in a folder of the associated model, not the upload model every time
You'll want to alias the model, like so:
//ArticleData MODEL
public $hasMany = array(
'AliasModelHere' => array(
'className' => 'Upload',
'foreignKey' => 'foreign_key',
'conditions' => array(
'Upload.model' => 'ArticleData',
),
),
);
// Then
$this->ArticleData->AliasModelHere->save($data)

Related

How we can add php code to model file in CAKE PHP

public $hasOne = array(
'Subscriber' => array(
'className' => 'Subscriber',
'foreignKey' => 'UserID',
'conditions' => array(
'Subscriber.end_date >=' => date('Y-m-d')
),
));
Here date('Y-m-d') gives html parse error. Even echo and print is not working in model file.
My aim is to get data from all subscribed users table till current date.
You can try like this,
public $hasOne = array(
'Subscriber' => array(
'className' => 'Subscriber',
'foreignKey' => 'UserID',
'conditions' => array(
'Subscriber.end_date >= date("Y-m-d")'
),
));
It will work as per your requirement

CakePHP get id value from input select

I have theCelebrant andCelebration models where aCelebrant has NCelebration. I am populating an input select in view of Celebration add that lists all registered Celebrant. Until then all right, the problem is that when you make the form submission the value of the select is coming as the number of select the viewing position, and I would like to come Celebrant ID.
Here's what I did:
CelebrationsController
$this->set('celebrants', $this->Celebration->Celebrant->find('list'));
view/Celebrations/add
echo $this->Form->input('celebrant_id', array('type' => 'select', 'label'=>'Celebrante', 'options' => $celebrants, 'empty' => __( 'Selecione um Celebrante' )));
model/Celebrant
var $hasMany = array(
'Celebration' => array(
'className' => 'Celebration',
'foreignKey' => 'celebrant_id'
)
);
model/Celebration
var $belongsTo = array(
'Celebrant' => array(
'className' => 'Celebrant',
'foreignKey' => 'celebrant_id'
)
);

CakeDC search plugin pagination not working

Hi I'm currently using the CakeDC search plugin for my CakePHP (2.2 app).
The search facility itself works fine, however I cannot get the results or the data shown on page before the search to paginate.
Heres my code
Model:
// Configure Meio file uploader
var $actsAs = array(
'MeioUpload.MeioUpload' => array(
'filename' => array(
'allowedMime' => array('image/jpeg', 'image/pjpeg', 'image/png', 'application/pdf'),
'allowedExt' => array('.jpg', '.jpeg', '.png', '.pdf'),
'maxSize' => '8 Mb'
)
),
'Search.Searchable'
);
// Search plugin filters
public $filterArgs = array(
'title' => array('type' => 'like'),
'live' => array('type' => 'value'),
'search' => array('type' => 'like', 'field' => 'FaqArticle.description'),
'error' => array('type' => 'like'),
'description' => array('type' => 'like')
);
Controller:
// Search plugin
public $components = array('Search.Prg');
public $presetVars = true; // using the model configuration
public function admin_index() {
$this->Prg->commonProcess();
$this->paginate = array('conditions' => $this->FaqArticle->parseCriteria($this->passedArgs));
$this->set('faqArticles', $this->paginate());
// Count all live articles for intro text
$this->set('liveArticles', $this->FaqArticle->find('count', array('conditions' => array('FaqArticle.live' => '1')
)));
// Count all articles for intro text
$this->set('countedArticles', $this->FaqArticle->find('count'));
// Set searched for details
$this->set('searchedFor', $this->passedArgs);
// Set layout
$this->layout = 'admin';
}
View:
<div class="pagination">
<ul>
<?php
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('tag' => 'li', 'separator' => '', 'currentClass' => 'active', 'modulus' => '5'));
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
?>
</ul>
this code:
public $components = array('Search.Prg');
public $presetVars = true; // using the model configuration
Should be in your controller, not your model. Models don't have 'components'. Controllers have 'components'. And check the doco for the Serach plugin - the $filterArgs goes in the model (which you've done correctly), but the $presetVars goes in the controller.
Don't set $presetVars as an empty array. Just move it from your model to your controller. It should be declared up the top as a public var, as you've done in your model, and shouldn't be declared inside the admin_index method.

CakePHP 2.0: display associated records

I know the solutions is simple and might have something to do with the Containable behavior but I can't get it working. Without all the tries
This is the case. I'd like to display the Event details of an Event (eg. a conference). Each event takes place in a EventVenue and each EventVenue is located in a Country.
So in the Country Model the following is present:
public $hasMany = array(
'EventVenue' => array(
'className' => 'EventVenue',
'foreignKey' => 'country_id'
))
In the EventVenue model a BelongsTo association is made
public $belongsTo = array(
'Country' => array(
'className' => 'Country',
'foreignKey' => 'country_id'
))
And in the Event model a hasOne association is made
public $hasOne = array(
'EventVenue' => array(
'className' => 'EventVenue',
'foreignKey' => 'event_id',
))
What I want is to display the country name on the page that is renderd in the EventsController. I do get all the Event and EventVenue data but the associated Country for the venue is not retrieved.
The data is retrieved in the following way
$item = $this->Event->findBySlug($slug);
How can I also get the country name (eg. Netherlands) retrieved from the database? I tried something like this but that did not work:
$item = $this->Event->findBySlug($slug,array(
'contain' => array(
'Event' => array(
'EventVenue' => array(
'Country'
)
)
)
)
Try this:
$item = $this->Event->findBySlug($slug,array(
'contain' => array(
'EventVenue' => array(
'Country'
)
)
);
Update
Turns out findBy does not support Containable. You could use this to get the desired result:
$item = $this->Event->find('first',array(
'conditions' => array(
'Event.slug' => $slug
),
'contain' => array(
'EventVenue' => array(
'Country'
)
)
);
Oh and make sure you have this in the model: public $actsAs = array('Containable');
try this method:
$this->Event->recusive = 2;
$item = $this->Event->findBySlug($slug);
You need to set the recursive to 2 before you make the find, something like this
$this->Event->recursive = 2;
with this, you'll get the Event, the EventVenue and the Country on one shot
Hope it helps

CakePhp: On the fly associations using a re-named Model field?

I'm trying to use on the fly associations to trim down the data I retrieve, but the model I'm using is associated to other models with a re-named field because I have 2 of the same models associated with it.
So, here's the model, say 'test', that has two 'user' fields, both related to the User model.
In the model:
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
),
'User_Watched' => array(
'className' => 'User',
'foreignKey' => 'user_id_watched'
)
);
When I retrieve data related to 'test', I want to only retrieve particular data linked to the 'User' and 'User_Watched' fields without any other nested information.
But when I do:
$this->User->unbindModel(array('hasMany' => array('something1', 'something2')), false);
then something1 and something2 data does not show up for the 'User' field of model 'test', but is still retrieved for the 'User_watched' field.
Can I not retrieve unwanted data for the 'User_watched' field?
Hope this makes sense... :)
KcYxA,
Containable behavior might help a lot in this case, as benjamin mentioned, your "find" queries would look like:
$this->User->find('first', array(
'conditions' => array('User.id' => $id),
'contain' => array('UserWatched')
));
In this case, you won't have to use unbindModel method. In this example, you'll get User and UserWatched data.
If you need only User data from "find", then tell Cake to "$this->User->contain();" so it won't go further then User model.
to use on the fly associations to trim
down the data I retrieve
Good idea.
'foreignKey' => 'user_id_watched'
should possibly be:
'foreignKey' => 'user_watched_id'.
Edit 1: At least this would make sense according to my current understanding. If user_id is a correct foreign key(FK), which cakephp uses to unbind the relations, but user_id_watched isn't, than your described behavior is explained.
Edit 2: The Containable behavior gives you another tool for controlling associated models.
Change $primaryKey in fly, run controller
Sample:
// Models
//....
class PreProductoDescripcion extends AppModel {
/**
* Primary key field
*
* #var string
*/
public $primaryKey = 'id_producto_descripcion';
//....
//....
}
class SenasaPedidosDetalles extends AppModel {
/**
* Display field
*
* #var string
*/
public $displayField = 'cod_tango';
public $belongsTo = array(
'SenasaPedidos' => array(
'className' => 'SenasaPedidos',
'foreignKey' => 'senasa_pedidos_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'PreProductoDescripcion' => array(
'className' => 'PreProductoDescripcion',
'foreignKey' => 'cod_tango',
//'conditions' => array('SenasaPedidosDetalles.cod_tango' => 'PreProductoDescripcion.codigo'),
'fields' => '',
'order' => ''
)
);
//....
#
// Controller Fly
//...
$this->SenasaPedidos->Behaviors->load('Containable');
$this->SenasaPedidos->SenasaPedidosDetalles->PreProductoDescripcion->primaryKey = 'codigo';
$datos = $this->SenasaPedidos->find(
'first', array(
'fields' => array( 'SenasaPedidos.*' ),
'conditions' => array( 'SenasaPedidos.id' => $id ),
'contain' => array(
'Usuarios' => array(
'fields' => array( 'Usuarios.apellido_nombre' )
),
'Clientes' => array(
'fields' => array( 'Clientes.razon_social' )
),
'Provincias' => array(
'fields' => array( 'Provincias.nombre' )
),
'Transportes' => array(
'fields' => array( 'Transportes.razon_social' )
),
'SenasaPedidosDetalles' => array(
'fields' => array( 'SenasaPedidosDetalles.*' ),
'PreProductoDescripcion' => array(
'fields' => array(
'PreProductoDescripcion.id_producto_descripcion',
'PreProductoDescripcion.descripcion'
)
)
),
)
));
//...

Resources