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.
Related
**USER MODEL**
public function post(){
return $this->hasOne('App\Post','user_id','id');
}
**WEB ROUTE**
use App\User;
Route::get('/{id}/post',function($id){
return User::find($id)->post;
});
Hi everyone, I'm fairly new to both PHP and Laravel and have been struggling a bit. I just have 2 questions for this code.
In the web routes, why doesn't post have any () beside it? It was declared a function in the user model. And.. I am unsure of how these relationships work (correct me if I am wrong) but does the code above look for a user with a specific $id and connects it with a post having a similar $user_id value?
For the first bit, it is a dynamic property, here you can find how you can actually make one yourself Laravel: create a dynamic property. They essentially work because the result is a single object search based on the id, since it doesn't have to retrieve a collection it allows itself to be accessed like an attribute of the object.
And yea pretty much on the second one. It also uses laravels models to retrieve the data from the database so that you get an object back without needing to create the repositories yourself.
There are major differences between User::find($id)->post and User::find($id)->post(). First one is returning the result of the related relations so you get the post that has user_id equal to $id.
The second one returns a query builder,so you can add more conditions. For example User::find($id)->post()->where("status", 1)->get().
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));
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
I'm slightly confused and need a bit of guidance if someone wouldn't mind.
Just a bit of background, my application is a website containing a simple survey (a list of questions with possible responses retrieved from the database) Thus far, I have created the models for the questions and responses tables and have defined the relationships within these classes - i.e., a question can have multiple possible responses whereas a response belongs to one question. I've used Eloquent to handle the retrieval of the data which works fine.
Now, I need to run a query using the query builder to retrieve the posted data because of some complex left joins that I cannot do using Eloquent, but I'm uncertain where to put these - do I put the query inside of a new model, or run it from my controller like I have when using Eloquent?
That code should be wrapped in a method of the model.. So if you need to do a crazy join for a user.. it would be $user->getSomeCrazyJoinedData()... you can also use a query scope if you want to pass arguments into it.
Is there a best practice in getting data from multiple database tables using Zend? I would like to know rather than end up wanting to refactor the code I write in the near future. I was reading the Zend documentation and it said that:
"You can not specify columns from a
JOINed tabled to be returned in a
row/rowset. Doing so will trigger a
PHP error. This was done to ensure
the integrity of the Zend_Db_Table is
retained. i.e. A Zend_Db_Table_Row
should only reference columns derived
from its parent table."
I assume I therefore need to use multiple models -- is that correct? If, for example, I want to get out all orders for a particular user id where the date is in between two dates what would I do?
I know that it would be possible to access the two different models from a controller and then combine their respective data in the action but I would not feel happy doing this since I have been reading survivethedeepend.com and it tells me that I shouldn't do this...
Where, why, and how? :)
Thanks!
If you're reading ZFSTDE, in chapter 9 (http://www.survivethedeepend.com/zendframeworkbook/en/1.0/implementing.the.domain.model.entries.and.authors) this problem is addressed by using a data mapper.
Also, you can join 2 tables, just be sure to first call on the select object the setIntegrityCheck(false) method. The docs say that a row should reference a parent table, doesn't mean it can not :)
Stop thinking about Zend_Db_Table as your "model".
You should write your own, rich, domain-centric model classes to sit between your controllers (and views), and your persistence logic (anything that uses Zend_Db/Zend_Db_Table/Zend_Db_Select) to load/store data from the database.
Sure, you can query several db tables at the same time. Take a look at the official ZF docs here http://framework.zend.com/manual/en/zend.db.select.html#zend.db.select.building.join
As for your example with getting all orders of a single user, table relationships are the answer http://framework.zend.com/manual/en/zend.db.table.relationships.html