Object Oriented PHP Query (dabl ORM) - php

I'm trying to use DABL ORM and its object oriented query building with a join however the results are only coming back from the first table I specify, can anybody advise what I'm doing wrong.
https://manifestwebdesign.com/redmine/projects/dabl/wiki/Object_Oriented_Query_Building
$stu = new Students;
$q = new Query;
$q->addColumn(Students::ADNO);
$q->join(Students::SEN, SenStatus::ID);
$q->addColumn(SenStatus::STATUS);
$students = $stu->doSelect($q);
Results when var_dump'd only show columns from students table.
There is no issue with the database structure as the normal sql query:
SELECT adno, status FROM students LEFT JOIN sen_status ON students.adno = sen_status.id
Works fine. Any thoughts?

have you tried all the syntax alternatives?
like
$students = Students::doSelect($q) ?

Related

Cross Join with php codeigniter query

Is there away to cross join from php. In example:
Currently I query a database like so:
$company_id = 20;
$templates_data = $this->db->select('template_id')
->from('dr_template_relational')
->where('dr_template_relational.company_id',$company_id)
->get()
->result_array();
What I'm looking to do is something like this:
->from('dr_template_relational')
->cross_join()
There's several responses to this question on SO but post reference a regular sql query like so:
"SELECT * FROM citys LEFT JOIN comments ON comments.city=citys.city WHERE citys.id=$id";
This would be the way to do it in SQL query but the point here is to do it in php and get the data returned with a cross join. I also realize the query can be made with in php to have it select the data and join it with code but my question is related to is there away to simply add ->cross_join() or something like that.
You can run raw query in codeigniter to solve your problem as below:
$sql 'your query here with cross join';
$query = $this->db->query($sql);
return $query->result_array();
Hope it helps you :)
You can use CI join method.
$company_id = 20;
$templates_data = $this->db->select('dr_template_relational.template_id')
->where('dr_template_relational.company_id',$company_id)
->join('table','dr_template_relational.company_id=table.company_id','LEFT')
->get()
->result_array();
where 'LEFT' is the join type

Laravel query with temporary table

How to convert this query to laravel db query.
SELECT * FROM {
Select * from organizers
Order by organizers.rank
} Group by t.department
This is simplified version of query. In real the inner query has more where clause and built using laravel db query.
Edit: I am aware of raw query. But that's not what I am looking for. Inner query is complex and has lots of conditional where clause. I would like to retain the db query object I used there.
You can have 2 different query builders and merge their binding like below :
$innerQuery = DB::table('organizers')->orderBy('organizers.rank');
$mainQuery = DB::table(DB::raw('(' . $innerQuery->toSql() . ') as t'))
->mergeBindings($innerQuery->getQuery())
->groupBy('t.department')
->get();
This will also help you retail the $innerQuery builder instance for your later use as you have mentioned in the question.
I think you will have to execute a raw query.
$result = DB::select("SELECT * FROM (
Select * from organizers
Order by organizers.rank
) Group by t.department");
reference: https://laravel.com/docs/5.7/queries#raw-expressions

Join on get_where - is this possible?

I have a table called Listings and a table called wines and one more called wineinfo
I originally was using the following to get the info from the listings table only, how ever since I restructured my DB, it requires the use of two other tables.
$listing = $this->db->get_where( "listings", [ "listingID" => $id ]
)->row();
if( !$listing )
throw new Exception("Error: Listing you're trying to bid on does not exist.", 1);
// check not to bid on own listings
if( $listing->list_uID == $userid )
throw new Exception("Error: Dont't bid on your own listings.", 1);
I then tried changing the code so the JOIN statements could work
$this->db->select("FROM listings.*, Vintage, Vineyard, Wine_Name, Region, Advice, Grape,Producer,Type id,wine_id,Wine_Name,");
$this->db->from("wineinfo");
$this->db->where(["listingsID" => $id]);
$this->db->where(["wineinfo.wine_id" => "listings.wine_id"]);
$this->db->where(["wineinfo.Vintage" => "listings.wine_id"]);
$this->db->join("wines", "wineinfo.wine_id = wines.wine_id");
$listing = $this->db->get()->row();
I am being given this error.
Unknown table 'listings'
But there is 100% a table called listings.
I know I've missed something, or definitely messed up the code, I am only just learning about this and the code above worked for something else, but now I've amended it to this, it hasn't.
I then tried changing the code so the JOIN statements could work
you are trying to combine 3 tables with 2 FROM and one JOIN clauses, which is incorrect the way you do it.
you need to keep SELECT clean, just select the columns you need, like:
$this->db->select("listings.*, wineinfo.*, wine.*");
then the FROM clause:
$this->db->from("wineinfo");
then make the joins:
$this->db->join("listings", "wineinfo.wine_id = listings.listingsID");
$this->db->join("wines", "wineinfo.wine_id = wines.wine_id");
followed by your where clauses.
please note, I don't know your table structure, so I can only guess your JOIN relationships. Also this is a simplified example, where I suppose that the 3 tables don't have matching column names.
response to "ambiguous" comment: you can limit your select clause to just necessary columns, e.g.
$this->db->select("listings.*, wineinfo.vintage, wine.*");
or use an alias:
$this->db->select("listings.*, wineinfo.wine_id as my_wineID, wine.*");
The FROM in $this->db->select("FROM listings.*, Vintage, Vineyard, Wine_Name, Region, Advice, Grape,Producer,Type id,wine_id,Wine_Name,"); is not supposed to be there.
The generated out query now looks like:
SELECT FROM *... FROM sometable

Query with database link server with pdo in php

I'm facing a problem right now, i explain :
I have my SQL query :
SELECT COUNT(*) AS nb, TO_CHAR(myDate,'yyyy-mm-dd') AS dateF, T1.IDCar, T2.IDMotor
FROM TDB2#MYLINKSERVER,T1
JOIN T2 ON T1.ID = T2.ID
WHERE TDB2.IDCar = T1.IDCar
Which use a TDB2#MYLINKSERVER , to connect 2 differents databases from Oracle and use a table from and other database.
In fact, when i launch the query in sql developper, my query returns some datas.
So in php, i have
$sql = $pdo->prepare('myquerybefore')
$sql->execute(array($annee));
$res = $sql->fetchAll(PDO::FETCH_ASSOC);
And when i try to vardump my $res, it returns array(0) { }
So i think that PDO don't use the #LINKSERVER to fetch results.
Any help would be appreciated
Sleakerz,
I found a way to make my query work :
I create a synonym with
CREATE PUBLIC SYNONYM LINKDATABASE
FOR TABLE1#THELINKDATABASE;
Then i have in my query: FROM LINKDATABASE which is my synonym !

Joining Data From Multiple Tables

I never did such PHP/MYSQL tricks to join multitables. Please who has experience in this field Help:
$qry=mysql_query("select {$table_prefix}user_cv.*, {$table_prefix}advertising.* from {$table_prefix}user_cv, {$table_prefix}advertising where {$table_prefix}user_cv.publish='yes' and {$table_prefix}advertising.publish='Y'");
mysql query return 0 results .
$qry = "SELECT {$table_prefix}user_cv.*, {$table_prefix}advertising.*
FROM {$table_prefix}user_cv
LEFT JOIN {$table_prefix}advertising ON {$table_prefix}advertising.publish='Y'
WHERE {$table_prefix}user_cv.publish='yes'";
mysql_query($qry);
I did not test this! I could be wrong but it might be a step in the right direction. (Still new myself)

Categories