different with insert active records - php

Can someone explain simply the difference with
$this->actions->add_record($some)
and
$this->db->insert($some)
as far as I am aware they both insert data, but im not sure the difference and cant find a good explanation online

$this->actions->add_record($some);
is user defined function
but
$this->db->insert($some);
is a built in function of Codeigniter's Query Builder Class and inserts data into a table like this:
$this->db->insert('users',array('name'=>'john));

Related

What is a simple process to define contain with find function

Care to put light to the subject. Issue is that when I do what ever to code I find that code does not go further to retrieve data from table Sectors and table courses. I have applied find() I use CakePHP 4.x to make clear that it is latest version of CakePHP called Strawberry. I also do not understand that why does my code get affected while I apply simple code structure to run my code.
Take a look at code snippet to run a simple find query with CakePHP Strawberry.
$Sectors is not defined but I need to know a way to define and apply $Sectors variable so that I have given value for relative table structure.
Table Does not load because of a problem I do not understand yet but guys help me out I'll update my code and make use of real code that is to determine a structure for code that will work for CakePHP Strawberry.
code:
$this->loadModel('Sectors');
$this->loadModel('Courses');
$Sectors = $Sectors->find('all')->contain('Sectors', 'Courses');
pr($sectors);
$this->contain(['Sectors', 'Courses']);
$this->set(compact('sectors'));
To retrieve data from table so that I get data relation from Sectors and Courses table.
error:
Call to a member function find() on null
Answer is simple but problem is CakePHP would keep single model with multiple query but not multiple model with single query.

yii ActiveRecord always querying database

Using Yii 1 and ActiveRecord -
I'm pretty new in both.
$types = CasinoSgTypes::model();
$types->findAll();
$types->findByPk(3);
I thought Yii AR will try to search in recently received data first instead of that I've got 2 calls to the database. Probably I'm using it in a wrong way?
sure i can walk through the array of results received by the first query (findAll) manually, but I'd like to do this by means of AR.
in other words is there a way to force AR search inside already received data and only then ask the database or smth like this. how to use AR+Yii models in a right way to avoid unnecessary queries ?
When you call $types->findAll(); the data isn't saved in the AR class and as such you cannot search using it. An easier alternative to searching in the data is to use query caching:
$types->cache(3600)->findByPk(3);

Cakephp Find Convert to Sql Syntax

Cakephp find data in controller also can with sql syntax,
example :
$this->Post->find('all');
in sql :
select * from posts
convert query to cakephp find syntax is available at http://dogmatic69.com/sql-to-cakephp-find-converter
but whether there is any link for me if I wanna convert my cakephp find to query..
Thanks in advance...
If I got what you are asking, here is the answer
SQL calls that you can’t or don’t want to make via other model
methods can be made using the model’s query() method (though this
should only rarely be necessary).
query() uses the table name in the query as the array key for the returned data, rather than the model name. For example:
$this->POST->query("SELECT* from posts;");
Here is the link for cakephp documentation

CakePHP Paginate() the results from a find subquery?

This is a follow-up to an earlier question here: CakePHP find statement for a sql query involving two models
I wasn't sure whether to add it on to there, but I figured this is a new question in itself. My apologies if this isn't correct!
Given a cakephp find subquery, is it possible to use paginate() on the results of the find? from the cake documentation, it seems that the paginate() function only works on Models, not on an array of data.
Thank you!
if you already have all the data in the array, I suggest output all that to your page (not the same as showing all the data) and use javascript to paginate it (just use a javascript grid plugin out there): more responsive app and less load on the server.

codeigniter find by library?

Is there a codeigniter plugin that allows me to quickly create find by functions without writing code on every field in a db table?
I find myself writing a lot of functions for tables such as findbyid findbyfirstname findbyemail and so on, any libraries already written to speedup my dev time? i tried googling but i havent come across any.
If you mean you have to write multiple methods in your model to find rows in a table by a specific field, you could just pass an associative array containing the fields and values you want to search to a generic function - something like:
function search_mytable($search=array()) {
$this->db->select('mytable.*');
$this->db->from('mytable');
if(!empty($search)
$this->db->where($search);
}
There's more information about what you can pass the CI active record where method here http://codeigniter.com/user_guide/database/active_record.html#select
If it's just simple data retrieval, you can just do something like this:
function find($column, $value)
{
$this->db->where($column, $value);
//etc
}
for simple queries. As BrynJ suggests, the Active Record class is rather flexible when it comes to taking parameters.

Categories