Transaction in cakephp- 3.0 - sql

In my cakephp3.0 application registration page the registration data is inserting into two tables. I am using query builder for doing this. After the successful insertion of data into the first table(Projects table) projectId(primary key of Projects table which is auto incremented value) will return and which is used to insert into second table that is ProjectCustomers table, in projectCustomers table projectId is used as forign key. My controller look like this
public function initialize() {
parent::initialize();
$this->Project = new ProjectsTable();
$this->ProjectCustomer = new ProjectCustomersTable();
}
public function add() {
if ($this->request->is('post')) {
$project_id = $this->Project->putProject($formData['survey_id'], $formData['title'], $formData['operator'], $this->Auth->user('id'));
$this->ProjectCustomer->putProjectCustomer($project_id, $formData['air_id'], $formData['email'], $formData['name'], $formData['company_name'], $formData['department_name'], SYS_ADMIN, PROJECT_CUSTOMER_STATUS_ACTIVE, $date, $date);
return $this->redirect(['action' => 'view', $project_id]);
}
}
What I need is that I want to make this two insertion action in a transaction so that if second insertion is failed anyhow, the first insertion in Projects table should roll back. Thank in advance for the help...

Related

Create value with prefix and auto increment in migration using laravel

I want to create a database table in laravel using migration, I have 4 columns in that table
1) ID(Auto-Increment, Primary Key)
2) Book Name
2) Book ID
4) Price
Now, I need to automatically fill value of BOOK ID column, value like this
Book ID = 'Book_1' (here "Book_" is prefix & 1 is value from ID column)
so for auto increment we create like this
$table->increments('id');
I need for BookID, how to write for that.
possible solution.
NOTE : not in table creation (migration) but when actually storing data.
create an ordinary varchar column to store name.
$table->string('name');
in the AppServiceProvider class boot() function. do something like this.
let's imagine your particular model is 'Book'
Book::created(function ($book) {
$book->update(['name' => 'Book_' . $book->id]);
}
this will bind an event to the 'Book' creation. when every time new book is saved to the database, name will automatically generate and save.
If the BookID column is only for the representational purpose you can add an accessor.
public function getBookIdAttribute()
{
return 'Book_' . $this->attributes['id'];
}
else, you can accomplish by adding this to your model,
protected static function boot()
{
parent::boot();
static::created(function ($model) {
$model->BookID = 'Book_' . $model->id;
$model->save();
});
}

Laravel/SQL: How to fetch data from multiple table in a single query? that too using 'where'

Working on a search functionality on Laravel App(Blog/Posts).
There are multiple types of posts (each having a separate table in the database)
Like Business posts, Social Life posts etc..
Below is the search function on SearchController
class SearchController extends Controller
{
public function search(Request $request, $query = null)
{
if($query == null)
return redirect()->route('home');
$search = Business::where([['title','like','%'.$query.'%'],['status','=',1]])
->orWhere([['description','like','%'.$query.'%'],['status','=',1]])
->paginate(10);
return view('front.search',[
'results' => $search,
'query' => $query
]);
}
}
So basically my question is how to add other types of Post's table also?
My main motive is that when someone searches for anything, the result should be fetched from all types of posts table(business, nature, life & so on..).
You have to maintain common id in both the table
NOTE: Join is the preferable method
$querys = DB::table('Business')->where([['Business.title','like','%'.$query.'%'],['Business.status','=',1]])
->orWhere([['Business.description','like','%'.$query.'%'],['Business.status','=',1]]);
$querys->join('socialtable','socialtable.userid','=','Business.userid');
// Just join the social table
$querys->where('socialtable.title', 'like','%'.$query.'%');
$result = $querys->paginate(10);
If you have a model called Book, like this:
class Book extends Model
{
/**
* Get the author that wrote the book.
*/
public function author()
{
return $this->belongsTo('App\Author');
}
}
Then you can retrieve all of your books with authors like this:
$books = App\Book::with(['author'])->get();
Check out Eager loading from Laravel documentation.
Just add table name before every field
$querys = DB::table('Business')->where([['Business.title','like','%'.$query.'%'],['Business.status','=',1]])
->orWhere([['Business.description','like','%'.$query.'%'],['Business.status','=',1]]);
$querys->join('socialtable','socialtable.userid','=','Business.userid');
// Just join the social table
$querys->where('socialtable.title', 'like','%'.$query.'%');
$result = $querys->paginate(10);

Codeigniter populate nested associations

this is the results i need from the database
{comment: 'hello there', user: [{name: 'sahan', id: 1}], id: 2}
function used to get comments
public function get_comments(){
$query = $this->db->get('comments');
return $query->result_array();
}
I have a comments table and a users table, When a user comments on something the
comment is saved as follows
comment > Comment text
user: userid
So when the data is shown I need codeigniter to populate the user field with the user data found from the users table
Does anyone know how to do this ?
I used this functionality in SailsJS but dont know how to do it here in CodeIG
Sails.js populate nested associations
Codeigniter 's active record is not as advanced as SailJS active record, but you can achieve what you are asking for with two queries within the same method.
I'm assuming the comments table has a foreign key to the users table through a user_id field.
public function get_comments() {
/* Get all the comments */
$query = $this->db->get('comments');
$comments = $query->result_array();
/* Loop through each comment, pulling the associated user from the db */
foreach $comments as &$comment {
$query = $this->db->get_where('users', array('id' => $comment['user_id']));
$user = $query->result_array();
/* Put the user's data in a key called 'user' within the comment array */
$comment['user'] = $user;
/* Remove the unnecessary user_id and $user variable */
unset($comment['user_id']);
unset($user);
}
return $comments;
}

CRUD very slow with conditions, is there any other faster way?

im having problem with CRUd now that i filled the database. CRUD is taking ages to show, becouse it takes condition from M:M tables.
Tables:
Table USER. has many labels (hasMany)
Table LABLE, has many users (hasMany)
Intermidiate Table UserLabel, has two hasOne
I want to show all users from some label with CRUD like this:
MODEL USER:
class Model_User extends Model_Table {
public $table ='user';
function init(){
parent::init();
$this->addField('fbid')->mandatory('Facebook id required');
...
$this->hasOne('Application');
$this->hasMany('UserLabel');
$this->addExpression('ratio')->set(function($model,$select){
return $select->expr('ROUND(([f2] / [f1]) * 100,0)')
->setCustom('f1',$model->getElement('sends'))
->setCustom('f2',$model->getElement('clicked'));
});
$this->addHook('beforeSave',function($m){
$m['updated']=$m->dsql()->expr('now()');
});
}
MODEL LABEL:
class Model_Label extends Model_Table {
public $table ='label';
function init(){
parent::init();
$this->addField('name')->mandatory('Name required');
$this->addFIeld('application_id')->refModel('Model_Application')->defaultValue($this->api->recall('app'))->system(true);
$this->addField('active')->type('boolean')->defaultValue('true')->system(true);
$this->addField('created')->type('timestamp')->defaultValue($this->dsql()->expr('now()'))->system(true);
$this->addField('updated')->type('timestamp')->system(true);
$this->hasMany('UserLabel');
$m = $this->add("Model_UserLabel");
$this->addExpression("users", $m->dsql()
->field($m->dsql()->expr("count(*)"), "all users")
->where("label_id", $this->getField("id"))
);
MODEL USER LABEL
class Model_UserLabel extends Model_Table {
public $table ='userlabel';
function init(){
parent::init();
$this->hasOne('User');
$this->hasOne('Label');
}
}
CODE FOR CRUD
$c = $this->add('CRUD');
$c->setModel('User', array('name', 'gender','country','city'));
$c->model->addCondition('id','in',
$this->add('Model_UserLabel')->addCondition('label_id', $_GET['l'])->dsql()->field('user_id')
);
Is there any better way to do this?
ps. I tested this solution, it is a lot faster but still very slow at around > 5.000 users:
//get all users
$records = $this->api->db->dsql()->option('distinct')->table('user')->join('userlabel.user_id')->field('user.id')->where('userlabel.label_id',$_GET['l'])->do_getAll();
foreach($records as $record){
$users .= ','.$record['id'];
}
//create CRUD
$c = $this->add('CRUD');
$c->setModel('User', array('name', 'gender','country','city','sends','clicked','ratio'));
$c->model->addCondition("application_id", $this->api->recall('app'));
$c->model->addCondition('id','in',
'('.$users.')'
);
Source code express more than words, so you better add your model definition source code (maybe not full) in your question.
What should be one row in your CRUD/Grid? I guess it's not 1 user = 1 row, but 1 user_label should be one row in grid. So you should set UserLabel model as model for your grid.
And then define some additional fields in Model_UserLabel by joining them from user and/or label tables directly like this:
class Model_UserLabel extends SQL_Model {
function init() {
parent::init();
// ...
// fields from user table
$join_u = $this->join('user', 'user_id');
$join_u->addField('username'); // this adds fields in current model from joined table
$join_u->addField('email');
// fields from label table
$join_l = $this->join('label', 'label_id');
$join_l->addField('name');
}
}
Note: source code above is untested and put here only as example.
EDIT:
Try this solution - almost the same as I wrote earlier above:
MODEL USER LABEL
class Model_UserLabel extends Model_Table {
public $table ='userlabel';
function init(){
parent::init();
$this->hasOne('User');
$this->hasOne('Label');
// join user table and add fields to this model from joined user table
$j = $this->join('user', 'user_id');
$j->addField('name');
$j->addField('gender');
$j->addField('country');
$j->addField('city');
}
}
CODE FOR CRUD
$m = $this->add('Model_UserLabel'); // UserLabel here not User
$m->addCondition('label_id', $_GET['l']); // and then this is simple
$c = $this->add('CRUD');
$c->setModel($m, array('name', 'gender','country','city'));
Try this solution and as (almost) always - code is untested.
EDIT:
Please try this version - is it working faster? That's basically your P.S. example, but you shouldn't extract all user IDs, join them and then create huge select with a lot of 'in'.
Faster result should be if you could do all with just one DB request without any additional processing of data.
// parameters
$app_id = $this->api->recall('app);
$label_id = $_GET['l'];
// prepare model for grid
$m = $this->add('Model_User'); // default User model
$m->_dsql()->option('distinct') // add join to userlabel table + conditions
->join('userlabel.user_id')
->where('userlabel.label_id', $label_id)
->where($m->getField('application_id'), $app_id);
// create CRUD and set it's model. All conditions already set on model above
$c = $this->add('CRUD');
$c->setModel($m, array('name', 'gender','country','city','sends','clicked','ratio'));
NOTE: Source code above as often - untested :)

phalcon resultset complex direct use

I have two tables and I am using phalcon's phql to join them.
In my controller i have:
$oBuilder = $this->modelsManager->createBuilder();
$oBuilder->columns(['Tabone.*', 'Tabtwo.*']);
$oBuilder->from(['Tabone']);
$oBuilder->join('Tabtwo', 'Tabone.id = Tabtwo.id');
$oBuilder->where('Tabone.id = 1');
$aRecords = $oBuilder->getQuery()->execute();
/** #var Phalcon\Mvc\Model\Resultset\Complex $aRecords */
//this doesnt work as expected
$aRecords[0]->tabone->setVal(2);
echo "2 != ".$aRecords[0]->tabone->getVal()."<br>";
echo get_class($aRecords[0]->tabone).'<br>';
//this works as expected
$aRecords->getFirst()->tabone->setVal(2);
echo "2 == ".$aRecords->getFirst()->tabone->getVal()."<br>";
So, with the Phalcon's Complex Traversable resultset I am able to set properties using :
$resultset->getFirst()->tabone->setVal(2);
echo $resultset->getFirst()->tabone->getVal();
But when i try :
echo get_class($aRecords[0]->tabone); // Says tabone
$resultset[0]->tabone->setVal(2);
echo $resultset[0]->tabone->getVal();
the value remains unchanged. even though $aRecords[0]->tabone is the class Tabone.
These are my models
class Tabone extends \Phalcon\Mvc\Model
{
public $id;
public $val;
public function columnMap() {
return array( 'id' => 'id', 'val' => 'val' );
}
public function setVal($val) { $this->val = $val; }
public function getVal() { return $this->val; }
}
class Tabtwo extends \Phalcon\Mvc\Model
{
public $id;
public function columnMap() {
return array( 'id' => 'id' );
}
}
these are the mysql tables and values
CREATE TABLE tabone (
id INT(11) NOT NULL AUTO_INCREMENT,
val INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (id)
);
CREATE TABLE tabtwo (
id INT(11) NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO tabone (id, val) VALUES (1, 1);
INSERT INTO tabtwo (id) VALUES (1);
Why are the setters/getters no working when using [0] ?
Am i doing something i shouldn't ? ...
because it is how it works. you have methods for these things available like:
offsetGet() // Gets row in a specific position of the resultset
getFirst() // Get first row in the resultset
getLast() // Get last row in the resultset
all methods are here: http://docs.phalconphp.com/en/latest/api/Phalcon_Mvc_Model_Resultset_Complex.html
it's good practice to not use array's key, to keep it simple imagine this:
you are using setters & getters, instead simply setting var's value. But when you want to implement new validation for some input field, you have to go through all the code where you set value, not only just edit your setter. i believe it has some same logic going on here, but i am not developing core of the phalcon, i if you want to get more details you should go check their C code here: https://github.com/phalcon/cphalcon
With information found on:
http://forum.phalconphp.com/discussion/945/why-properties-of-models-are-lost-
(...) when a resultset is traversed, only just one record is kept in memory,
if you modify a record changes will lost, because the record is freed
once it is not used anymore. This scheme is very efficient if you are
traversing big resultsets (...)
and on
Scala: What is the difference between Traversable and Iterable traits in Scala collections?
(...) complying with the Traversable interface does not require
keeping state
So, the reason why [0] does not set properties is because traversable means just
that, it only traverses the object, any values set directly in the traversed object
will be lost, because the object state is not kept.
This makes perfect sense especially when you are talking about large result sets
as it will save tons of memory.