AutoIncrement PK incrementing irregularry - mysql

I recently release an app: https://my.kendozone.com in Laravel 5.3 / MySQL
I'm getting users, but one weird thing is that my user's id are not incrementing by one.
HERE last 10 rows id :
5401
5402
5403
5404
5405
5406
5407
5408
8359
9799
The 2 ways I save my users are :
$user = User::create([
'provider' => $provider,
'provider_id' => $userData->id,
'name' => $userData->name,
'firstname' => $userData->name,
'slug' => str_slug($userData->name),
'email' => $userData->email,
'avatar' => $avatar,
'role_id' => config('constants.ROLE_USER'),
'verified' => 1,
]);
or
$user = User::create(['name' => $request->name,
'email' => $request->email,
'password' => bcrypt($request->password),
'role_id' => config('constants.ROLE_USER'),
'verified' => 1,
]);
How should understand this??? Is it normal???
Does anybody have an explanation???

My id are weird because of my Functional tests.
when running, they use :
use DatabaseTransactions
that mean that all queries runs into a Transaction, but I guess it uses ids range.

Related

Yii2 update with multiple conditions

$this->localdb->createCommand()
->update(
$this->MYTable,
[
'name' => $el['new'],
'data' => $el['data'],
],
[
'userId', $this->user,
'product_id', $this->productId,
'name', $el['old'],
'created', $el['date'],
'category', $el['cat'],
]
);
I tried to use the following command to update a row using multiple where conditions, but it doesn't work for some reason. The documentation doesn't seem to cover this case and doesn't seem to be updated, because the update() method seems to match the updateAll method instead of the update method for some reason (maybe it was updated?). So I was wondering what was the correct way to do this.
You have a syntax error. Try following:
$this->localdb->createCommand()
->update(
$this->MYTable,
[
'name' => $el['new'],
'data' => $el['data'],
],
[
'userId' => $this->user,
'product_id' => $this->productId,
'name' => $el['old'],
'created' => $el['date'],
'category' => $el['cat'],
]
);
Try This updateAll query :
MYTable::updateAll([ // field to be updated in first array
'name' => $el['new'],
'data' => $el['data']
],
[ // conditions in second array
'userId' => $this->user,
'product_id' => $this->productId,
'name' => $el['old'],
'created' => $el['date'],
'category' => $el['cat']
]);

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

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.

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.

Datatables : Make a parallel request in another table using ServerSide

EDIT : I found the solution by replacing the SSP class by a customized SSP class I've found here : https://github.com/emran/ssp
I don't know if it's an understandable title, but here is my problem :
I have a DB-table (called projects) that needs to be inserted in a datatable. I've no problem to make a call using ServerSide and get results in the datatable.
But, for each project (each row), there is a project creator (column creator_id in the projects DB-table). What I need to do is to make a request to the creators DB-table in order to get the firstname/lastname of the creator, each time I get a row from the projects DB-table. Is that make sense?
Here is the code I use :
$table = 'projects';
$primaryKey = 'project_id';
$columns = array(
array(
'db' => 'project_id',
'dt' => 'DT_RowId',
'formatter' => function( $d, $row ) {
return $d;
}
),
array( 'db' => 'creator_id',
'dt' => 'creator',
'formatter' => function( $d, $row ) {
// Here I need to make a call to the creators DB-table and return the $creator_name value
return $creator_name;
}
)
);
// SQL server connection information
$sql_details = array(
'user' => '',
'pass' => '',
'db' => '',
'host' => ''
);
require(BASE_DIR.'/lib/dataTables/ssp.class.php');
$result = SSP::simple($_GET, $sql_details, $table, $primaryKey, $columns);
You can use formatter property to make a separate SQL query but it will affect performance greatly by increasing script response time.
Instead you can use the following trick when using ssp.class.php with sub-queries instead of table name to use GROUP BY, JOIN, etc.
<?php
$table = <<<EOT
(
SELECT projects.project_id, creators.creator_id, creators.creator_name
FROM projects
LEFT JOIN creators ON projects.creator_id=creators.creator_id
) t
EOT;
$primaryKey = 'project_id';
$columns = array(
array(
'db' => 'project_id',
'dt' => 'DT_RowId'
),
array(
'db' => 'creator_id',
'dt' => 'creator'
),
array(
'db' => 'creator_name',
'dt' => 'creator_name'
)
);
// SQL server connection information
$sql_details = array(
'user' => '',
'pass' => '',
'db' => '',
'host' => ''
);
require(BASE_DIR.'/lib/dataTables/ssp.class.php');
$result = SSP::simple($_GET, $sql_details, $table, $primaryKey, $columns);
echo json_encode($result);
To use that trick, you also need to edit ssp.class.php and replace all instances of FROM `$table` with FROM $table to remove backticks.
Alternatively, there is github.com/emran/ssp repository for library that extends ssp.class.php allowing GROUP BY, JOIN, aliases.
LINKS
See jQuery DataTables: Using WHERE, JOIN and GROUP BY with ssp.class.php for more information.

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.