Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6598319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:19:54+00:00 2026-05-25T18:19:54+00:00

I have to get all the associative data for the model. Basically the UNION

  • 0

I have to get all the associative data for the model. Basically the UNION query is as below:

  SELECT * FROM `videos` AS `U1` 
  WHERE `U1`.`level_id` = '1' AND `U1`.`submitted_date` > '2011-09-11'
UNION 
  SELECT * FROM `videos` AS `U2`
  WHERE `U2`.`level_id` = '1' AND `U2`.`submitted_date` < '2011-09-11'
  ORDER BY  submitted_date DESC
  LIMIT 0,10

So when I use
$this->Video->find('all')--> I get all the result of the associated table datas.

Now I want to use the UNION SQL query and that should return associated table data too…

Any ideas how to use the cake inbuilt function to get data?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:19:54+00:00Added an answer on May 25, 2026 at 6:19 pm

    You can do this in 4 or more different ways… the easiest but not recomended is using

    $this->Model->query($query); 
    

    where $query is the query stated above.

    The second way but may not be what you want, is to redo your sql query you will get same result (but not separated with the alias) like this:

    SELECT * FROM `videos` AS `U1` 
    WHERE `U1`.`level_id` = '1' AND (`U1`.`submitted_date` > '2011-09-11' OR `U1`.`submitted_date` < '2011-09-11')
    ORDER BY  submitted_date DESC
    LIMIT 0,10
    

    This query can be easily done with find like this

    $conditions = array(
        'Video.level_id'=>1,
        'OR' => array(
            'Video.submitted_date <'=> '2011-09-11',
            'Video.submitted_date >'=> '2011-09-11'
        )
    );
    $this->Video->find('all', array('conditions'=>$conditions)) 
    

    The third way will be the one that Abba Bryant talk about, explained in detail here Union syntax in cakePhp that works building the statement directly.

    The fourth way will like the first one more less, you will have to create a behaviour that have a beforeFind function and there you will have to check if a option union and create the query or to create something like the the third option.

    you will call it with a find like this

    $this->Video->find('all', array('conditions'=>$conditions, 'union'=> $union));
    

    This will be something more less like the linkable or containable behavior.

    The fith way is to modified your cakephp sql driver… this one, i don’t really know the changes you have to do, but it is a way to get to that… This drivers are the responsible to interpret and create the queries, connect to db and execute the queries…

    REMEMBER that cakephp find do the checks neccesary to prevent SQLInyection and other risks… the $model->query will NOT do this tests so be carefull

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query to get all the compatible phones in a specific country,
I have to create an SQL Query to get all rows starting with a
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a
I need get all items these have no categories int? categoryId = null; var
I have a written a method to get all the records and return in
I have got some code to load an assembly and get all types, which
I have this code to connect to Active Directory and get all the groups
Hi I have a 2 tables BlogPost and BlogComments I want to get all
I have a set of .EXE commands. How do I get all those commands
So, i have some question about xml Documents in Java. Can i get all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.