Linking two models with CakeDC Search Plugin - mysql

I have a small problem that is me consuming days.
I need made a search in two diferent models in CakePHP, i'm using CakeDC Search Plugin.The models are 'Desordens' and 'Sinonimos', the association is hasMany. One 'Desorden' hasMany 'Sinonymous':
public $hasMany = array(
'Sinonimo' => array(
'className' => 'Sinonimo',
'foreignKey' => 'desorden_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
),
So, i follow the example in CakeDC Docs an made this :
public $filterArgs = array(
'busca' => array(
'type' => 'like',
'encode' => true,
'before' => false,
'after' => false,
'field' => array(
'Desorden.name',
'Desorden.orphanumber',
'Sinonimo.sinonimo')
)
);
What i want to do is a search where the user can insert the name,orphanumber or the synonymous.In SQL would be:
SELECT `Desorden`.`id`, `Desorden`.`name`, `Desorden`.`orphanumber`, `Desorden`.`expertlink`, `Desorden`.`tipodesordem`, `Desorden`.`descricao`, `Sinonimo`.`id`, `Sinonimo`.`sinonimo`, `Sinonimo`.`desorden_id` FROM `rederaras`.`desordens` AS `Desorden` LEFT JOIN `rederaras`.`sinonimos` AS `Sinonimo` ON (`Sinonimo`.`desorden_id` = `Desorden`.`id`) WHERE ((`Desorden`.`name` LIKE 'mult') OR (`Desorden`.`orphanumber` LIKE 'mult') OR (`Sinonimo`.`sinonimo` LIKE 'mult'))
But the table 'Sinonimo' its not being joined and i have the 1054 error:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Sinonimo.sinonimo' in 'where clause'
I don't know what i missing, the controller i have configured like this:
$this->Prg->commonProcess();
$this->Desorden->recursive = 0;
$this->Paginator->settings['conditions'] = $this->Desorden->parseCriteria($this->Prg->parsedParams());
$this->set('desordens', $this->Paginator->paginate());
I think its no problem in the model or cakedc filterArg configuration, because the query is constructed in the right way.I try to use bindModel to made a join between the tables 'Desordens' and 'Sinonimos' but no success....
$this->Desorden->bindModel(array(
'hasMany' => array(
'Sinonimo' => array(
'foreignKey' => false,
'conditions' => array('Desorden.id = Sinonimo.desorden_id')
)
)
), false);
Anyone can help me to made this join ?
Thanks

I solved the problem using $belongsTo in the two Models. When i set $belongsTo the CakeDC create a Join between the tables. I don't know if this a best solution, but works for me. If this help someone.

Related

Call to undefined method Varien_Db_Statement_Pdo_Mysql::addColumn()

When i try to run my upgrade script it gives Call to undefined method Varien_Db_Statement_Pdo_Mysql::addColumn() error on second addcolumn but when i remove all other column and keep only one addColumn it work fine.My upgrade script is as below
$installer->startSetup();
/**
* alter table 'savecart/savecart'
*/
$installer->getConnection()
->addColumn($installer->getTable('savecart/savecart'),'savecart_name', array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'nullable' => true,
'length' => 255,
'comment' => 'Savecart Name'
))
->addColumn($installer->getTable('savecart/savecart'),'savecart_comment', array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'nullable' => true,
'comment' => 'Savecart Comment'
))
->addColumn($installer->getTable('savecart/savecart'),'savecart_bill_id', array(
'type' => Varien_Db_Ddl_Table::TYPE_INTEGER,
'nullable' => true,
'length' => 10,
'comment' => 'Billing Id'
))
->addColumn($installer->getTable('savecart/savecart'),'savecart_valid_till', array(
'type' => Varien_Db_Ddl_Table::TYPE_DATE,
'nullable' => true,
'comment' => 'Valid Till Date'
));
$installer->endSetup();
CHAINING CORRECTLY
I was looking into this and found several sites that use a different writing method:
http://magedevguide.com/challenge/chapter4/1
http://magento.ikantam.com/qa/how-setup-magento-scripts
Please try these methods with your values..
You can try copy pasting following code, but please review it for your values..
$installer->startSetup();
/**
* alter table 'savecart/savecart'
*/
$installer->getConnection()
->newTable($installer->getTable('savecart/savecart'))
->addColumn('savecart_name', Varien_Db_Ddl_Table::TYPE_TEXT, 255, array('nullable' => true), 'Savecart Name')
->addColumn('savecart_comment', Varien_Db_Ddl_Table::TYPE_TEXT, null, array('nullable' => true), 'Savecart Comment')
->addColumn('savecart_bill_id', Varien_Db_Ddl_Table::TYPE_INTEGER, 10, array('nullable' => true), 'Billing Id')
->addColumn('savecart_valid_till', Varien_Db_Ddl_Table::TYPE_DATE, null, array('nullable' => true), 'Valid Till Date');
$installer->endSetup();
Please let me know if one of the solutions is working so I can update my answer.
Please use following upgrade script add multiple columns to the existing magento table.for detailed explanation.
https://www.pearlbells.co.uk/upgrade-script-multiple-columns-magento/
$installer->getConnection()
->addColumn($installer->getTable('imagedetail'),'filters_workshop_colour',
array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'nullable' => true,
'comment' => 'Filters Workshop Colour'
));
$installer->getConnection()
->addColumn($installer->getTable('imagedetail'),'splashbackcolor',
array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'nullable' => true,
'comment' => 'Splash Back Color'
));
$installer->getConnection()
->addColumn($installer->getTable('imagedetail'),'cabinetcolor',
array(
'type' => Varien_Db_Ddl_Table::TYPE_TEXT,
'nullable' => true,
'comment' => 'Cabinet Color'
));

cakephp3.0 pagination giving error for join table

I am using cakephp 3.0 and trying to pagination result set from two tables
$this->paginate = array(
'fields' => array(
'ModifierGroups.id',
'ModifierGroups.name',
'ModifierGroups.type',
'ModifierGroups.house_id',
'Houses.name',
),
'conditions' => $search_filter,
'limit' => 18,
'order' => ['ModifierGroups.name' => 'ASC'],
'recursive'=>-1,
);
$this->paginate('ModifierGroups')
but it's giving me this error
Column not found: 1054 Unknown column 'Houses.name' in 'field list'
ModelTable
$this->belongsTo('Houses', [
'foreignKey' => 'house_id'
]);
you should use contain to include 'Houses', associated models are no longer lazy loaded. recursive is also gone in CakePHP3
http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#eager-loading-associations

Complex CakePHP query refactoring

I have just inherited a project and I need to re-work a bit of code so that the pagination of an infinite scroller still work properly.
Right now the code is grabbing all of the categories and their products, and listings. I need to edit it so that only categories that have, products, which have active listings are returned.
Here is the code that eventually worked:
$catData = $this->find('all',array(
'conditions' => array(
'Indexer.type' => 'Category',
'Listing.listing_id IS NOT NULL'
),
'joins' => array(
array('table' => 'peeka_product_category_link',
'alias' => 'Link',
'type' => 'LEFT',
'conditions' => array(
'Link.category_id = Category.category_id'
)
),
array('table' => 'peeka_products',
'alias' => 'Product',
'type' => 'LEFT',
'conditions' => array(
'Product.product_id = Link.product_id'
)
),
array('table' => 'peeka_listings',
'alias' => 'Listing',
'type' => 'LEFT',
'conditions' => array(
'Listing.product_id = Product.product_id',
'Listing.listing_end_date >=' => $date,
'Listing.listing_start_date <=' => $date,
"Listing.listing_status = 'Active'"
)
),
),
'order' => 'Category.category_name ASC',
'limit' => $set_limit,
'fields' => array('Category.category_id, Category.category_name, Indexer.url'),
'group' => 'Category.category_id',
'recursive' => 0
));
EDIT: Thanks to Dave this is working now and I just wanted to post it for future reference. Maybe it will help someone else.
"...only categories that have, products, which have active listings are
returned."
"...a way to combine these three queries into one, so that the first
Category->find() function retrieves all the valid data."
To retrieve data and restrict it based on fields of associated models, you'll want to use CakePHP's JOIN.
It's hard to answer your question further than that without just writing the code for you, so - give JOINs a try, then come back and ask another more specific question if you have any issues with it.

Zend Framework 2 - Multiple sub domains cause problems

I am writing an application in Zend Framework 2 which is going to run from a few different subdomain, and I want to have a different module for each sub domain, to keep things tidy.
My problem is when I add more than 1 sub domain to the routing, it loses one of the sub domains.
eg: This setup works
testbed.localhost (module/Application)
a.testbed.localhost (module/A)
If I add an extra one it will the route all requests for a to the Application Index Controller
eg
testbed.localhost (module/Application), a.testbed.localhost (module/A), b.testbed.localhost (module/B)
This is the module.config.php for module/A
'router' => array(
'routes' => array(
'ads' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'a.testbed.localhost',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'__NAMESPACE__' => 'A\Controller',
'controller' => 'A\Controller\A',
'action' => 'index',
),
),
And this is the route in module.config.php in module/B
'router' => array(
'routes' => array(
'ads' => array(
'type' => 'Hostname',
'options' => array(
'route' => 'b.testbed.localhost',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'__NAMESPACE__' => 'B\Controller',
'controller' => 'B\Controller\B',
'action' => 'index',
),
),
Now the namespaces are correct in both the module.config.php files, what I have noticed is the sub domain a.testbed.localhost will work if I remove the reference to it from config/application.config.php
<?php
return array(
'modules' => array(
'Application',
'A',
'B', <--- A doesn't work if B is here
),
And if I swap A & B around in the modules array above, then B will get forwarded to the Application Module and A will work. So it seems to have problems with more than 1 sub domain. Has anyone got any ideas / come across the same thing?
This happens because your route names are the same. I would try a-ads and b-ads for route names and that should resolve your situation.
In the end the configuration is getting merged together. So it's like an array, when the last array is merged it overwrites anything before it.

Wordpress Multiple meta_key (WPDB or WP_Query)

Attempting to test whether a post has a 'main_slider', 'flickr-slider', or 'video-slider' value. 'main_slider' is a String, 'flickr-slider' and 'video-slider' are both Boolean.
This is what I have so far, which doesn't work a lick...
$slider = new WP_Query(
array(
'ignore_sticky_posts' => 1,
'post_type' => 'any',
'orderby' => 'date',
'nopaging' => true,
'posts_per_page' => 10,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'main_slider'
),
array(
'key' => 'flickr-slider'
),
array(
'key' => 'video-slider'
)
)
)
);
Thank you for any help in advance.
I decided to go with a workaround. I called each meta_key independently, merged while stripping out duplicate post, and then sorted by date. I'm sure its a heavier load on the server, but it got the job done.