Cakephp 3 save associations dose not work - php

I try to make a blog with articles and tags but for some reason my tags wont save when creating articles.
I did followed some ideas from official cake blog and from here but no luck for me .. Maybe something I do wrong and I dont see.
Tables for keywords
CREATE TABLE `keywords` (
`id` int(11) NOT NULL,
`article_id` int(11) DEFAULT NULL,
`tag` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Table model
Keywords:
class KeywordsTable extends Table
{
......................
public function initialize(array $config)
{
parent::initialize($config);
$this->table('keywords');
$this->displayField('id');
$this->primaryKey('id');
$this->belongsTo('Article', [
'foreignKey' => 'article_id',
'joinType' => 'INNER'
]);
}
....................
}
Article
class ArticleTable extends Table
{
public function initialize(array $config)
{
parent::initialize($config);
$this->table('article');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
$this->hasMany('Keywords', [
'foreignKey' => 'article_id'
]);
}
......................................
}
And here is my controller:
public function add()
{
//$this->autoRender = false;
$article = $this->Article->newEntity();
if ($this->request->is('post')) {
$article = $this->Article->patchEntity($article, $this->request->data, ['associated'=>['Keywords']]);
echo "<pre>";
print_r($this->Article->save($article));
die();
}
}
And this is the code I use in my form
<?= $this->Form->input('article[keywords][]', ['label'=>false, 'class' => 'form-control', 'placeholder' => 'keywords', 'templates' => ['inputContainer' => '{{content}}']]); ?>
So if any one could help me out I will appreciate.

Ok, after 4 hours I managed to find the problem.
Problem was in my view form input.
It should have been article.0.tag instead of article[keywords][]
Changed that and all works now.
Regards

in controller you should use associated array, more details-
$this->Article->save($article,['associated' => ['Keywords']]);
and in view file, make input field like this -
<?= $this->Form->input('keywords.0.title', ['label'=>false, 'class' => 'form-control', 'placeholder' => 'keywords', 'templates' => ['inputContainer' => '{{content}}']]); ?>
more details

Related

Saving an Association with CakeDC Users

I am using CakeDC Users and as part of the registration I would like the registrant to choose some checkbox values that correspond to another Model in the application. I am able to get the form to load the choices just fine, but I am unable to get it to save to the join table even though I have added all of the associations and accessibilities where it would seem relevant. The form is displaying in a similar way to other areas where I am saving the same type of association with a different model.
TypesTable.php
{
public function initialize(array $config): void
{
//$this->addBehavior('Timestamp');
$this->setDisplayField('type');
$this->setPrimaryKey('id');
$this->belongsToMany('Words');
$this->belongsToMany('Users');
$this->belongsTo('Languages', [
'foreignKey' => 'language_id',
'joinType' => 'INNER',
]);
}```
UsersTable.php (in the plugin folders)
```class UsersTable extends Table
{
...
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('users');
$this->setDisplayField('username');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->addBehavior('CakeDC/Users.Register');
$this->addBehavior('CakeDC/Users.Password');
$this->addBehavior('CakeDC/Users.Social');
$this->addBehavior('CakeDC/Users.LinkSocial');
$this->addBehavior('CakeDC/Users.AuthFinder');
$this->hasMany('SocialAccounts', [
'foreignKey' => 'user_id',
'className' => 'CakeDC/Users.SocialAccounts',
]);
$this->hasMany('Types', [
'foreignKey' => 'user_id', 'targetForeignKey' => 'type_id',
'joinTable' => 'types_users']);
}```
In order for a "belongsToMany" association to work in this instance, both *Table.php files must have the relations listed as belongsToMany. Don't forget to make the field accessible in the Entities as well.
One also needs to place the associated array in the patchEntity function in the RegisterBehavior.php file.
UserTable.php (in CakeDC users)
public function initialize(array $config): void
{
...
$this->belongsToMany('Types', [
'foreignKey' => 'user_id', 'targetForeignKey' => 'type_id',
'joinTable' => 'types_users']);
}
TypesTable.php (in app)
class TypesTable extends Table
{
public function initialize(array $config): void
{
....
$this->belongsToMany('Users');
$this->belongsTo('Languages', [
'foreignKey' => 'language_id',
'joinType' => 'INNER',
]);
}
RegisterBehavior.php
$user = $this->_table->patchEntity(
$user,
$data,
['validate' => $validator ?: $this->getRegisterValidators($options), 'associated' => ['Types']]
);

Cannot search based on multiple id in find query in Cakephp 3

I have two tables category and product. I want to get details of product whose id are passed in conditions array of find query. The problem is when i pass condition as an array, it returns
Cannot convert value to integer
Secondly, if i debug it by passing only one id it returns category record, but it should return product record. Please help to solve my issue.
My code is as follows:
public function display()
{
$ids = array(1,2,3,4,5);
$c_List = $this->Products->Categories->find('all',array('conditions'=>array('Categories.category_id'=>$ids)));
$data = $c_List->toArray();
debug($data);
die();
}
Model - ProductsTable.php
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('products');
$this->setDisplayField('product_id');
$this->setPrimaryKey('product_id');
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Categories', [
'foreignKey' => 'category_id',
'joinType' => 'INNER'
]);
}
Model - CategoriesTable.php
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('categories');
$this->setDisplayField('category_id');
$this->setPrimaryKey('category_id');
$this->belongsTo('Categories', [
'foreignKey' => 'category_id',
'joinType' => 'INNER'
]);
}
When you are finding the records based on an array of ids, use IN in the condition clause.
public function display()
{
$ids = array(1,2,3,4,5);
$c_List = $this->Products->Categories->find('all',array('conditions'=>array('Categories.category_id IN'=>$ids)));
$data = $c_List->hydrate(false)->toArray();
pr($data);
die();
}

How to save images with Cakephp-File-Storage?

UPDATE
So I added some logs to the action upload in ProductsController and the method upload in MediasTable to find out what is happening. The entity from ProductsController this->Products->Medias->newEntity() was pass to MediasTable but it wasn't save.
It is necessary to upload the file to save the data in the db? Like if all the data is ok but the file is no present the event will be reject the data and do nothing in the db?
I'm using cakephp 3.1 with the file-storage plugin. I'm following the quickstart guide from the docs: Quick-Start but I don't understand some parts and doesn't upload, insert in database neither make thumbnails.
This is my database:
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(255) NOT NULL,
quantity INT NOT NULL,
sold INT NOT NULL,
description VARCHAR(1000),
price DECIMAL(7,2) NOT NULL,
old_price DECIMAL(7,2) NOT NULL,
visited INT NOT NULL,
status INT NOT NULL,
created DATETIME,
modified DATETIME
);
CREATE TABLE media_types (
id INT AUTO_INCREMENT PRIMARY KEY,
name_media_type VARCHAR(255) NOT NULL,
created DATETIME,
modified DATETIME
);
CREATE TABLE medias (
id INT AUTO_INCREMENT PRIMARY KEY,
media_type_id INT NOT NULL,
product_id INT NOT NULL,
path VARCHAR(255) NOT NULL,
created DATETIME,
modified DATETIME,
FOREIGN KEY media_type_key (media_type_id) REFERENCES media_types(id),
FOREIGN KEY product_key (product_id) REFERENCES products(id)
);
MediasTable:
...
use Burzum\FileStorage\Model\Table\ImageStorageTable;
class MediasTable extends ImageStorageTable {
public function initialize(array $config) {
parent::initialize($config);
$this->table('medias');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('MediaTypes', [
'foreignKey' => 'media_type_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'joinType' => 'INNER'
]);
}
...
public function upload($productId, $entity) {
$media = $this->patchEntity($entity, [
'adapter' => 'Local',
'model' => 'Media',
'foreign_key' => $productId
]);
Log::write('debug', $media);
return $this->save($media);
}
}
ProductsTable:
class ProductsTable extends Table {
public function initialize(array $config) {
parent::initialize($config);
$this->table('products');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('Medias', [
'className' => 'Medias',
'foreignKey' => 'foreign_key',
'conditions' => [
'Medias.model' => 'Media'
]
]);
}
...
}
ProductsController:
class ProductsController extends AppController {
public function upload($productId = null) {
$productId = 2;
$entity = $this->Products->Medias->newEntity();
if ($this->request->is(['post', 'put'])) {
$entity = $this->Products->Medias->patchEntity(
$entity,
$this->request->data
);
if ($this->Products->Medias->upload($productId, $entity)) {
$this->Flash->set(__('Upload successful!'));
}
}
$this->set('productImage', $entity);
}
...
}
In config/local_store.php is the same as the example (I include this file in boostrap.php)
...
$listener = new LocalFileStorageListener();
EventManager::instance()->on($listener);
$listener = new ImageProcessingListener();
EventManager::instance()->on($listener);
Configure::write('FileStorage', [
'imageSizes' => [
'Medias' => [
'large' => [
...
]);
FileStorageUtils::generateHashes();
StorageManager::config('Local', [
'adapterClass' => '\Gaufrette\Adapter\Local',
'adapterOptions' => [TMP, true],
'class' => '\Gaufrette\Filesystem'
]);
upload.ctp
echo $this->Form->create($productImage, array('type' => 'file'));
echo $this->Form->file('file');
echo $this->Form->error('file');
echo $this->Form->submit(__('Upload'));
echo $this->Form->end();
In the quick start there is two upload methods: uploadImage and uploadDocument
but in the controller they use "upload".
There is another association in Products in the example, I need this?:
$this->hasMany('Documents', [
'className' => 'FileStorage.FileStorage',
'foreignKey' => 'foreign_key',
'conditions' => [
'Documents.model' => 'ProductDocument'
]
]);
I found this question (from there is the db I'm using) Getting Started with cakephp-file-storage quickstart guide and upload and insert but doesn't make the thumbnails and if I change the table to ImageStoreTable shows an error "Class not found"
So if anybody can help me I will be very grateful!

CakePHP 3 join table associations

Alright, I have some issues understanding how the associations are working, particularly belongsTo here is my setup:
Articles can have multiple Categories
Categories can belong to multiple Articles
so in my database i have 3 tables:
articles, categories and a join table articles_categories
Table/ArticlesTable.php:
public function initialize(array $config)
{
$this->addBehavior('Timestamp');
$this->table('articles');
$this->belongsTo('Users');
$this->belongsToMany('Categories', [
'through' => 'ArticlesCategories',
'alias' => 'Categories',
'foreignKey' => 'article_id',
'joinTable' => 'articles_categories',
'targetForeignKey' => 'category_id'
]);
}
Table/CategoriesTable.php:
public function initialize(array $config)
{
$this->table('categories');
$this->displayField('name');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsToMany('Articles', [
'through' => 'ArticlesCategories',
'alias' => 'Articles',
'foreignKey' => 'category_id',
'joinTable' => 'articles_categories',
'targetForeignKey' => 'article_id'
]);
}
Table/ArticlesCategoriesTable.php:
public function initialize(array $config)
{
$this->belongsTo('Articles');
$this->belongsTo('Categories');
}
Now inside in the view action CategoriesController.php i can overview a particular category and i need to retrieve some articles related to that category.
What is the right way to do such a thing? Here is what i have:
public function view($id = null)
{
$category = $this->Categories->find('all',['limit'=>1])->where(['Categories.id' => $id])->contain(['Articles']);
$this->set(['category'=> $category]);
}
It kinda does the job but I'd also need to be able to limit the number of related articles..
you can modify the query object used to load the associated models:
$category = $this->Categories->find('all',['limit'=>1])
->where(['Categories.id' => $id])
->contain(['Articles' => function($q) {
$q->limit(10);
return $q;
}
]);
edit: or you can do
$category = $this->Categories->get($id,
[
'contain' => [
'Articles' => function($q) {
$q->limit(10);
return $q;
}
]);
or maybe if you want the Articles without the Category data you can use matching
$articles = $this->Categories->Articles->find()
->matching('Categories', function ($q) use $id{
return $q->where(['id' => $id])
->limit(10);
I did not tested the last one but I think something like that should work
But as you can see the complexity is more o less the same

cakePHP 3.0 HABTM relations save data

I want to make this tutorial http://miftyisbored.com/complete-tutorial-habtm-relationships-cakephp/ in cakePHP 3.0
I have 3 tables: recipes, ingredients and ingredients_recipes.
When making a recipe, I want to select ingredients. Then I want to store the recipe_id and ingredient_id in the ingredients_recipes table, but fail to do so. I think there's something wrong in my RecipesController. Can someone help me or point me in the right direction?
Problem:
$ingredients = $this->Recipes->Ingredients->find('list', ['limit' => 200]);
// => THIS GIVES ME THE MESSAGE "The recipe could not be saved. Please, try again."
$ingredients = $this->Ingredients->find('list', ['limit' => 200]);
// => THIS GIVES ME THE ERROR "Call to a member function find() on boolean"
When I do var dump (when using this $this->Recipes->Ingredients->find) I get this:
array(3) {
["recipe_name"]=> string(4) "Test"
["recipe_description"]=> string(4) "Test"
["Recipe"]=> array(1) {
["Ingredient"]=> array(1) { [0]=> string(1) "1" }
}
}
Tables:
CREATE TABLE `recipes` (
`recipe_id` int(11) NOT NULL auto_increment,
`recipe_name` varchar(255) NOT NULL,
`recipe_description` text NOT NULL,
PRIMARY KEY (`recipe_id`)
);
CREATE TABLE `ingredients` (
`ingredient_id` int(11) NOT NULL auto_increment,
`ingredient_name` varchar(255) NOT NULL,
`ingredient_description` text NOT NULL,
PRIMARY KEY (`ingredient_id`)
);
CREATE TABLE `ingredients_recipes` (
`ingredient_id` int(11) NOT NULL,
`recipe_id` int(11) NOT NULL,
PRIMARY KEY (`ingredient_id`,`recipe_id`)
);
Here's my code below:
Model > Entity:
Recipe
class Recipe extends Entity
{
protected $_accessible = [
'recipe_id' => true,
'recipe_name' => true,
'recipe_description' => true,
];
}
Ingredient
class Ingredient extends Entity
{
protected $_accessible = [
'ingredient_id' => true,
'ingredient_name' => true,
'ingredient_description' => true,
];
}
IngredientsRecipe
class IngredientsRecipe extends Entity
{
protected $_accessible = [
'ingredient' => true,
'recipe' => true,
];
}
Model > Table :
RecipesTable
class RecipesTable extends Table
{
public function initialize(array $config)
{
$this->table('recipes');
$this->displayField('recipe_name');
$this->primaryKey('recipe_id');
$this->belongsTo('Recipes', [
'foreignKey' => 'recipe_id',
'joinType' => 'INNER'
]);
$this->belongsToMany('Ingredients', [
'className' => 'Ingredients',
'joinTable' => 'ingredients_recipes',
'foreignKey' => 'recipe_id',
'targetForeignKey' => 'ingredient_id'
]);
}
public function validationDefault(Validator $validator)
{
$validator
->requirePresence('recipe_name', 'create')
->notEmpty('recipe_name')
->requirePresence('recipe_description', 'create')
->notEmpty('recipe_description')
->requirePresence('Ingredients', 'create')
->notEmpty('Ingredients');
return $validator;
}
}
IngredientsTable
class IngredientsTable extends Table
{
public function initialize(array $config)
{
$this->table('ingredients');
$this->displayField('ingredient_name');
$this->primaryKey('ingredient_id');
$this->belongsTo('Ingredients', [
'foreignKey' => 'ingredient_id',
'joinType' => 'INNER'
]);
$this->belongsToMany('Recipies', [
'className' => 'Recipies',
'joinTable' => 'ingredients_recipes',
'foreignKey' => 'ingredient_id',
'targetForeignKey' => 'recipe_id'
]);
}
}
IngredientsRecipesTable
class IngredientsRecipesTable extends Table
{
public function initialize(array $config)
{
$this->table('ingredients_recipes');
$this->displayField('recipe_id');
$this->primaryKey(['recipe_id', 'ingredient_id']);
$this->belongsTo('Recipies', [
'foreignKey' => 'recipe_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Ingredients', [
'foreignKey' => 'ingredient_id',
'joinType' => 'INNER'
]);
}
Controller:
RecipesController
public function add()
{
$recipe = $this->Recipes->newEntity();
if ($this->request->is('post')) {
$recipe = $this->Recipes->patchEntity($recipe, $this->request->data);
// var_dump($this->request->data);
if ($this->Recipes->save($recipe)){
$this->Flash->success('The recipe has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The recipe could not be saved. Please, try again.');
}
}
$recipes = $this->Recipes->find('list', ['limit' => 200]);
$ingredients = $this->Recipes->Ingredients->find('list', ['limit' => 200]);
// => THIS GIVES ME THE MESSAGE "The recipe could not be saved. Please, try again."
$ingredients = $this->Ingredients->find('list', ['limit' => 200]);
// => THIS GIVES ME THE ERROR "Call to a member function find() on boolean"
$this->set(compact('recipe', 'recipes', 'ingredients'));
$this->set('_serialize', ['recipe']);
}
Template > Recipes
add.ctp
<?= $this->Form->create($recipe); ?>
<fieldset>
<legend><?= __('Add Recipe') ?></legend>
<?php
echo $this->Form->input('recipe_name', array(
'label' => 'Name'
)
);
echo $this->Form->input('recipe_description', array(
'label' => 'Description'
)
);
echo $this->Form->input('Recipes.Ingredients', ['multiple'=>true]);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
Maybe this tutorial could help: http://book.cakephp.org/3.0/en/tutorials-and-examples/bookmarks/intro.html.
It explains exactly how to set up HABTM relations, adding tags to bookmarks, and more.
Good luck and happy coding :)

Categories