ZF2 - \Zend\Db\Sql\Expression and DATE_FORMAT - mysql

I'm using Zend Framework 2.4 and trying to amend a working SQL columns select from
'content_publish_date' => 'publish_date',
to
'content_publish_date' => new \Zend\Db\Sql\Expression('DATE_FORMAT(\'content.publish_date\',\'%d/%c/%Y\')'),
produces the following SQL...
DATE_FORMAT('content.publish_date','%%d/%%c/%%Y') AS `content_publish_date`,
... which fails syntactically on MySQL (note the extra '%')
I have tried various alternatives...
'content_publish_date' => new \Zend\Db\Sql\Expression('DATE_FORMAT(content.publish_date,\'%d/%c/%Y\')'),
'content_publish_date' => new \Zend\Db\Sql\Expression('DATE_FORMAT(content.publish_date,"%d/%c/%Y")'),
... and so on, none of which has helped.
Does anyone have any idea how to use the Expression correctly to get the correct generated SQL?
The full code for this piece is as follows:
$dbSelect->join(
'content',
'content.id = category.content_id',
array(
'content_title' => 'title',
'content_html_title' => 'html_title',
'content_description' => 'description',
'content_content_text' => 'content_text',
'content_url' => 'url',
'content_url_vars' => 'url_vars',
'content_meta_keyword' => 'meta_keyword',
'content_meta_description' => 'meta_description',
'content_content_type_id' => 'content_type_id',
'content_disclaimer_id' => 'disclaimer_id',
'content_agent_target_id' => 'agent_target_id',
'content_video_position' => 'video_position',
'content_video_size' => 'video_size',
'content_creation_date' => 'creation_date',
'content_publish_date' => new \Zend\Db\Sql\Expression('DATE_FORMAT(\'content.publish_date\',\'%d/%c/%Y\')'),
'content_flag_content_url' => 'flag_content_url',
'content_flags' => 'flags'
),
\Zend\Db\Sql\Select::JOIN_LEFT
);
Thanks in advance.

Related

I want to use Jalali date in my Laravel / Angular database but I faced with this error: Whoops, looks like something went wrong

Recently I have purchased "Schoex - Ultimate school management system", a software for managing school. This software designed by Laravel 4 and AngularJS. Default date for this software is Gerigorian and I want to change it to Jalali date. I found different package from internet but not any one worked.
For example I tried to used this package: https://github.com/morilog/jalali
and It shows this error: Whoops, looks like something went wrong.
app.php
<?php
return array(
'debug' => false,
'log' => 'daily',
'url' => '',
'timezone' => 'Asia/Kabul',
'locale' => 'fa',
'key' => 'SchoexSchoolApplicationByCrHouse',
'providers' => array(
'Illuminate\Foundation\Providers\ArtisanServiceProvider',
'Illuminate\Auth\AuthServiceProvider',
'Illuminate\Cache\CacheServiceProvider',
'Illuminate\Session\CommandsServiceProvider',
'Illuminate\Foundation\Providers\ConsoleSupportServiceProvider',
'Illuminate\Routing\ControllerServiceProvider',
'Illuminate\Cookie\CookieServiceProvider',
'Illuminate\Database\DatabaseServiceProvider',
'Illuminate\Encryption\EncryptionServiceProvider',
'Illuminate\Filesystem\FilesystemServiceProvider',
'Illuminate\Hashing\HashServiceProvider',
'Illuminate\Html\HtmlServiceProvider',
'Illuminate\Log\LogServiceProvider',
'Illuminate\Mail\MailServiceProvider',
'Illuminate\Database\MigrationServiceProvider',
'Illuminate\Pagination\PaginationServiceProvider',
'Illuminate\Queue\QueueServiceProvider',
'Illuminate\Redis\RedisServiceProvider',
'Illuminate\Remote\RemoteServiceProvider',
'Illuminate\Auth\Reminders\ReminderServiceProvider',
'Illuminate\Database\SeedServiceProvider',
'Illuminate\Session\SessionServiceProvider',
'Illuminate\Translation\TranslationServiceProvider',
'Illuminate\Validation\ValidationServiceProvider',
'Illuminate\View\ViewServiceProvider',
'Illuminate\Workbench\WorkbenchServiceProvider',
'Tappleby\AuthToken\AuthTokenServiceProvider',
'Morilog\JalaliServiceProvider',
),
'manifest' => storage_path().'/meta',
'aliases' => array(
'App' => 'Illuminate\Support\Facades\App',
'Artisan' => 'Illuminate\Support\Facades\Artisan',
'Auth' => 'Illuminate\Support\Facades\Auth',
'Blade' => 'Illuminate\Support\Facades\Blade',
'Cache' => 'Illuminate\Support\Facades\Cache',
'ClassLoader' => 'Illuminate\Support\ClassLoader',
'Config' => 'Illuminate\Support\Facades\Config',
'Controller' => 'Illuminate\Routing\Controller',
'Cookie' => 'Illuminate\Support\Facades\Cookie',
'Crypt' => 'Illuminate\Support\Facades\Crypt',
'DB' => 'Illuminate\Support\Facades\DB',
'Eloquent' => 'Illuminate\Database\Eloquent\Model',
'Event' => 'Illuminate\Support\Facades\Event',
'File' => 'Illuminate\Support\Facades\File',
'Form' => 'Illuminate\Support\Facades\Form',
'Hash' => 'Illuminate\Support\Facades\Hash',
'HTML' => 'Illuminate\Support\Facades\HTML',
'Input' => 'Illuminate\Support\Facades\Input',
'Lang' => 'Illuminate\Support\Facades\Lang',
'Log' => 'Illuminate\Support\Facades\Log',
'Mail' => 'Illuminate\Support\Facades\Mail',
'Paginator' => 'Illuminate\Support\Facades\Paginator',
'Password' => 'Illuminate\Support\Facades\Password',
'Queue' => 'Illuminate\Support\Facades\Queue',
'Redirect' => 'Illuminate\Support\Facades\Redirect',
'Redis' => 'Illuminate\Support\Facades\Redis',
'Request' => 'Illuminate\Support\Facades\Request',
'Response' => 'Illuminate\Support\Facades\Response',
'Route' => 'Illuminate\Support\Facades\Route',
'Schema' => 'Illuminate\Support\Facades\Schema',
'Seeder' => 'Illuminate\Database\Seeder',
'Session' => 'Illuminate\Support\Facades\Session',
'SSH' => 'Illuminate\Support\Facades\SSH',
'Str' => 'Illuminate\Support\Str',
'URL' => 'Illuminate\Support\Facades\URL',
'Validator' => 'Illuminate\Support\Facades\Validator',
'View' => 'Illuminate\Support\Facades\View',
'AuthToken' => 'Tappleby\Support\Facades\AuthToken',
'AuthTokenNotAuthorizedException' => 'Tappleby\AuthToken\Exceptions\NotAuthorizedException',
'jDate' => 'Morilog\Facades\jDate',
'jDateTime' => 'Morilog\Facades\jDateTime',
),
);

Linking two models with CakeDC Search Plugin

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.

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.

Not all POST array elements getting sent

I have a table which has 4 columns with the first column containing a text input boxes. The entire table is wrapped inside a form that will run a PHP script on that data. As of right now, I'm just printing out the POST array. The problem is that I'm missing some of the values that I write into the input fields. So putting the values 1-10 in the first 10 text inputs gives me the following in print_r:
Array
(
[65710] =>
[65705] => 2
[269200318] =>
[269200586] => 4
[16830189] =>
[16829165] =>
[1051902] => 7
[65742] =>
[16830122] =>
[269339822] => 10
}
The HTML for this form can be found here: http://jsfiddle.net/nY9MF/
This is running on IIS 7
Edit: I updated the jsfiddle code to have the full table for one of these stores. This is the POST array after I hit submit:
Array(
[65710] =>
[65705] => 2
[269200318] =>
[269200586] => 4
[16830189] =>
[16829165] =>
[1051902] => 7
[65742] =>
[16830122] =>
[269339822] => 10
[65774] =>
[16821982] =>
[65740] =>
[16825517] =>
[65709] =>
[1051326] =>
[1052334] =>
[65773] =>
[4110] =>
[16837342] =>
[16825518] =>
[1051375] =>
[1051594] =>
[65743] =>
[1052108] =>
[256] =>
[4106] =>
[65711] =>
[65738] =>
[1051597] =>
[4109] =>
[65724] =>
[SaveQuantity] => Save
)