Codeigniter - Database select - mysql

Hi,
I need a help related to Codeigniter very badly.
I have some problems with a database query, i'am new to Codeigniter.
I have 3 tables:
1) film(film_id, filmname) PK (film_id)
2) category(category_id, categoryname) PK (category_id)
3) film_category(contains both primary keys (film_id) and (category_id))
The problem is that I want to select all filmname from film tables where category_id = 3.
How to do this with active record class?
Please make a suggestion.
I'am new to Codeigniter and I love it.
Thank you in advance from your friend.

Something like that:
$this->db->select('f.filmname');
$this->db->join('film_category fc', 'fc.film_id = f.film_id');
$this->db->where('fc.category_id', 3);
$query = $this->db->get('film f');
I find the ActiveRecord one of the most useful and elegant parts of CodeIgniter.

select data from the database in descending order
$this->db->order_by("id","desc");
$query = $this->db->get('table_name');
return $query->result();

Related

How to perform an update containing a sub query on an Eloquent Relationship in laravel 5

I am new to laravel. I'm working on this laravel 5 app but got stuck on trying to perform a query like below.
update applications
set application_status = 'sa'
where id in (select application_id from application_cart where cart_id = 12);
Appreciate help on how to translate this query.
The query below does the trick
return Application::whereIn('id', function($query) use ($id){
$query->from('application_cart')
->select('application_id')->where('cart_id', $id);
})->update(['application_status' => 'sa']);

multiple sql join in laravel 4.2

hi im fairly new in laravel 4.2 so i have this table (table 1) which gets data from a related table (table 2) but table 2 also gets from another related table (table 3) here is a visualization
im using laravel 4.2 query builder for this here is my sample code on how im connecting table 1 and table 2
$records = DB::table('table1')
->join('table2', 'table1.someID', '=', 'table2.someID')
->select('select something')
->get();
my problem is i don't know how to get the values from table 3 is there a way for this?
any help would be appreciated
Umm.. I don't really mind but, here you go. You just have to put another join to get the values to the third table from the table 2 judging from the visual above.
$records = DB::table('table1')
->join('table2', 'table1.someID', '=', 'table2.someID')
->join('table3', 'table3.someID', '=', 'table2.someID')
->select('select something')
->get();

Sql- how to use relational field in condition

I have three tables which is mapped like this: paymentDetails <-employee<-designation.
Now I have to get datas from paymentDetails table by particular designation of employee..
select *
from paymentDetails
where payment_date=date and employee.designation.desig_id=2;
And I am using Yii2 framework How can I achieve this in Yii2.
I get unknown column error. How to resolve this ?
$command = Yii::app()->db->createCommand()
->select(*)
->from('paymentDetails')
->where('payment_date=date')
->andWhere('employee.designation.desig_id=2')
select *
from paymentDetails
where payment_date=date and employee.designation.desig_id=2;
this will not work in SQL either, it is beacause you are using the tables employee and designation and you do not actually join them in any way.
Now you have not given us any details regarding the name of the models, but it should be something like
$paymentDetails = PaymentDetails::find()->joinWith('employee.designation')-where(['employee.designation.desig_id' => 2, 'payment_date' => 'date'])->all();
This will execute
select *
from paymentDetails JOIN employee ON 'theDefinedRelation' JOIN designation ON 'theSecondDefinedRelation' where payment_date=date and employee.designation.desig_id=2;
Anyway, it will be a long day, if you do not know why the SQL fails you have to learn SQL first.

Symfon2 database record inserion

I have a problem with symfony2 record insertion
My requirement is to find out all the users who are in between particular ages and the given column is dob.
I am getting the result with this mysql query.
SELECT * FROM app_users WHERE YEAR(CURDATE())-YEAR(dob) BETWEEN 10 AND 20;
How to rewrite this query in the symfony- doctrine fromat?
Pls help...
$qb = $em->createQueryBuilder();
$qb->select('au')
->from('AppUsers au)
->where($qb->expr()->between('YEAR(CURDATE())-YEAR(au.dob)', 10,20));
$result = $qb->getQuery()->getResult();
I have not checked it. but it may help you.

Symfony2 & Doctrine: Order entites by foreign attribute

I would like to use a select to get an array of entities with Doctrines QueryBuilder. But I need an ORDER BY which uses a foreign attribute (attribute in a table related with a foreign key). What I would like to write intuitively is something like this:
$repo = $this->getDoctrine()->getRepository('MyBundle:relation_table');
$query = $repo->createQueryBuilder('r')
->orderBy('r.fevent.date', 'DESC')
->getQuery();
Which, not surprisingly, doesn't work. In SQL my SELECT looks like this:
SELECT r.* FROM relation_table AS r
INNER JOIN events AS e
ON e.IDevent = r.Fevent
ORDER BY e.date
But I also need Doctrine to give me the entity-object back. I think of two possible solutions:
Use the QueryBuilder to create an INNER JOIN, or
Create a free SQL Statement, same as above, and tell Doctrine somehow to create an entity object with its results.
Any hints? Thanks.
You need to join the entity you want to order with:
$query = $repo->createQueryBuilder('r')
->join('r.fevent', 'f')
->orderBy('f.date', 'DESC')
->getQuery()
;
Joining both tables in your Doctrine Query (the first of your proposed solutions) is pretty straigtforward. Take a look here: http://symfony.com/doc/2.0/book/doctrine.html#joining-to-related-records
A similar question was answered here: Doctrine 2: How to search for an entity by its association's value?