cakephp3 own query builder












0















i'm new in cakephp, and i'm following the toutorial, i came from other languages and is not usual for me to read query like this:



   public function findTagged(Query $query, array $options)
{
$columns = [
'Articles.id', 'Articles.user_id', 'Articles.title',
'Articles.body', 'Articles.published', 'Articles.created',
'Articles.slug',
];

$query = $query
->select($columns)
->distinct($columns);

if (empty($options['tags'])) {
// If there are no tags provided, find articles that have no tags.
$query->leftJoinWith('Tags')
->where(['Tags.title IS' => null]);
} else {
// Find articles that have one or more of the provided tags.
$query->innerJoinWith('Tags')
->where(['Tags.title IN' => $options['tags']]);
}

return $query->group(['Articles.id']);
}


This is a simple query and it's easy to understand ,but if i have a more complex query with a lot of join etc, is there the possibility to write your own query with sql sintax, can you help me translating this code to a query written in sql?



Thanks










share|improve this question



























    0















    i'm new in cakephp, and i'm following the toutorial, i came from other languages and is not usual for me to read query like this:



       public function findTagged(Query $query, array $options)
    {
    $columns = [
    'Articles.id', 'Articles.user_id', 'Articles.title',
    'Articles.body', 'Articles.published', 'Articles.created',
    'Articles.slug',
    ];

    $query = $query
    ->select($columns)
    ->distinct($columns);

    if (empty($options['tags'])) {
    // If there are no tags provided, find articles that have no tags.
    $query->leftJoinWith('Tags')
    ->where(['Tags.title IS' => null]);
    } else {
    // Find articles that have one or more of the provided tags.
    $query->innerJoinWith('Tags')
    ->where(['Tags.title IN' => $options['tags']]);
    }

    return $query->group(['Articles.id']);
    }


    This is a simple query and it's easy to understand ,but if i have a more complex query with a lot of join etc, is there the possibility to write your own query with sql sintax, can you help me translating this code to a query written in sql?



    Thanks










    share|improve this question

























      0












      0








      0








      i'm new in cakephp, and i'm following the toutorial, i came from other languages and is not usual for me to read query like this:



         public function findTagged(Query $query, array $options)
      {
      $columns = [
      'Articles.id', 'Articles.user_id', 'Articles.title',
      'Articles.body', 'Articles.published', 'Articles.created',
      'Articles.slug',
      ];

      $query = $query
      ->select($columns)
      ->distinct($columns);

      if (empty($options['tags'])) {
      // If there are no tags provided, find articles that have no tags.
      $query->leftJoinWith('Tags')
      ->where(['Tags.title IS' => null]);
      } else {
      // Find articles that have one or more of the provided tags.
      $query->innerJoinWith('Tags')
      ->where(['Tags.title IN' => $options['tags']]);
      }

      return $query->group(['Articles.id']);
      }


      This is a simple query and it's easy to understand ,but if i have a more complex query with a lot of join etc, is there the possibility to write your own query with sql sintax, can you help me translating this code to a query written in sql?



      Thanks










      share|improve this question














      i'm new in cakephp, and i'm following the toutorial, i came from other languages and is not usual for me to read query like this:



         public function findTagged(Query $query, array $options)
      {
      $columns = [
      'Articles.id', 'Articles.user_id', 'Articles.title',
      'Articles.body', 'Articles.published', 'Articles.created',
      'Articles.slug',
      ];

      $query = $query
      ->select($columns)
      ->distinct($columns);

      if (empty($options['tags'])) {
      // If there are no tags provided, find articles that have no tags.
      $query->leftJoinWith('Tags')
      ->where(['Tags.title IS' => null]);
      } else {
      // Find articles that have one or more of the provided tags.
      $query->innerJoinWith('Tags')
      ->where(['Tags.title IN' => $options['tags']]);
      }

      return $query->group(['Articles.id']);
      }


      This is a simple query and it's easy to understand ,but if i have a more complex query with a lot of join etc, is there the possibility to write your own query with sql sintax, can you help me translating this code to a query written in sql?



      Thanks







      mysql cakephp-3.0






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 9:00









      picklerickpicklerick

      32918




      32918
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You can write directly execute an SQL query with $connection->execute() (https://book.cakephp.org/3.0/en/orm/database-basics.html#running-select-statements) but I would recommend to stick to cakephp's ORM.



          If you want to know how the query that you posted above translates into SQL, I would recommend to use the DebugKit. If you have in your app configuration debug = true, you will see this red rectangle at the bottem right corner, when you open your app in the browser. Click on it and click on "Sql queries": you will find the generated SQL from the query above somewhere in there. Alternatively you could use query logging (see here: https://book.cakephp.org/3.0/en/orm/database-basics.html#database-query-logging)






          share|improve this answer
























          • Thank you, but i'm trying to use debugkit but i'm missing smething ; i've enabled it in application.php, but i see nothing on the page, also added code in configuration: book.cakephp.org/3.0/en/debug-kit.html

            – picklerick
            Nov 24 '18 at 11:23













          • Difficult to guess what could be going on. In your config/bootstrap.php do you see this line Plugin::load('DebugKit', ['bootstrap' => true]); ? Is there a debug_kit.sqlite file in the tmp directory?

            – David Albrecht
            Nov 24 '18 at 11:39











          • no, the row is missing in my file.

            – picklerick
            Nov 24 '18 at 11:41











          • Solved! seems that you need to have a database called debug_kit

            – picklerick
            Nov 24 '18 at 14:34











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456674%2fcakephp3-own-query-builder%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can write directly execute an SQL query with $connection->execute() (https://book.cakephp.org/3.0/en/orm/database-basics.html#running-select-statements) but I would recommend to stick to cakephp's ORM.



          If you want to know how the query that you posted above translates into SQL, I would recommend to use the DebugKit. If you have in your app configuration debug = true, you will see this red rectangle at the bottem right corner, when you open your app in the browser. Click on it and click on "Sql queries": you will find the generated SQL from the query above somewhere in there. Alternatively you could use query logging (see here: https://book.cakephp.org/3.0/en/orm/database-basics.html#database-query-logging)






          share|improve this answer
























          • Thank you, but i'm trying to use debugkit but i'm missing smething ; i've enabled it in application.php, but i see nothing on the page, also added code in configuration: book.cakephp.org/3.0/en/debug-kit.html

            – picklerick
            Nov 24 '18 at 11:23













          • Difficult to guess what could be going on. In your config/bootstrap.php do you see this line Plugin::load('DebugKit', ['bootstrap' => true]); ? Is there a debug_kit.sqlite file in the tmp directory?

            – David Albrecht
            Nov 24 '18 at 11:39











          • no, the row is missing in my file.

            – picklerick
            Nov 24 '18 at 11:41











          • Solved! seems that you need to have a database called debug_kit

            – picklerick
            Nov 24 '18 at 14:34
















          1














          You can write directly execute an SQL query with $connection->execute() (https://book.cakephp.org/3.0/en/orm/database-basics.html#running-select-statements) but I would recommend to stick to cakephp's ORM.



          If you want to know how the query that you posted above translates into SQL, I would recommend to use the DebugKit. If you have in your app configuration debug = true, you will see this red rectangle at the bottem right corner, when you open your app in the browser. Click on it and click on "Sql queries": you will find the generated SQL from the query above somewhere in there. Alternatively you could use query logging (see here: https://book.cakephp.org/3.0/en/orm/database-basics.html#database-query-logging)






          share|improve this answer
























          • Thank you, but i'm trying to use debugkit but i'm missing smething ; i've enabled it in application.php, but i see nothing on the page, also added code in configuration: book.cakephp.org/3.0/en/debug-kit.html

            – picklerick
            Nov 24 '18 at 11:23













          • Difficult to guess what could be going on. In your config/bootstrap.php do you see this line Plugin::load('DebugKit', ['bootstrap' => true]); ? Is there a debug_kit.sqlite file in the tmp directory?

            – David Albrecht
            Nov 24 '18 at 11:39











          • no, the row is missing in my file.

            – picklerick
            Nov 24 '18 at 11:41











          • Solved! seems that you need to have a database called debug_kit

            – picklerick
            Nov 24 '18 at 14:34














          1












          1








          1







          You can write directly execute an SQL query with $connection->execute() (https://book.cakephp.org/3.0/en/orm/database-basics.html#running-select-statements) but I would recommend to stick to cakephp's ORM.



          If you want to know how the query that you posted above translates into SQL, I would recommend to use the DebugKit. If you have in your app configuration debug = true, you will see this red rectangle at the bottem right corner, when you open your app in the browser. Click on it and click on "Sql queries": you will find the generated SQL from the query above somewhere in there. Alternatively you could use query logging (see here: https://book.cakephp.org/3.0/en/orm/database-basics.html#database-query-logging)






          share|improve this answer













          You can write directly execute an SQL query with $connection->execute() (https://book.cakephp.org/3.0/en/orm/database-basics.html#running-select-statements) but I would recommend to stick to cakephp's ORM.



          If you want to know how the query that you posted above translates into SQL, I would recommend to use the DebugKit. If you have in your app configuration debug = true, you will see this red rectangle at the bottem right corner, when you open your app in the browser. Click on it and click on "Sql queries": you will find the generated SQL from the query above somewhere in there. Alternatively you could use query logging (see here: https://book.cakephp.org/3.0/en/orm/database-basics.html#database-query-logging)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 10:51









          David AlbrechtDavid Albrecht

          149111




          149111













          • Thank you, but i'm trying to use debugkit but i'm missing smething ; i've enabled it in application.php, but i see nothing on the page, also added code in configuration: book.cakephp.org/3.0/en/debug-kit.html

            – picklerick
            Nov 24 '18 at 11:23













          • Difficult to guess what could be going on. In your config/bootstrap.php do you see this line Plugin::load('DebugKit', ['bootstrap' => true]); ? Is there a debug_kit.sqlite file in the tmp directory?

            – David Albrecht
            Nov 24 '18 at 11:39











          • no, the row is missing in my file.

            – picklerick
            Nov 24 '18 at 11:41











          • Solved! seems that you need to have a database called debug_kit

            – picklerick
            Nov 24 '18 at 14:34



















          • Thank you, but i'm trying to use debugkit but i'm missing smething ; i've enabled it in application.php, but i see nothing on the page, also added code in configuration: book.cakephp.org/3.0/en/debug-kit.html

            – picklerick
            Nov 24 '18 at 11:23













          • Difficult to guess what could be going on. In your config/bootstrap.php do you see this line Plugin::load('DebugKit', ['bootstrap' => true]); ? Is there a debug_kit.sqlite file in the tmp directory?

            – David Albrecht
            Nov 24 '18 at 11:39











          • no, the row is missing in my file.

            – picklerick
            Nov 24 '18 at 11:41











          • Solved! seems that you need to have a database called debug_kit

            – picklerick
            Nov 24 '18 at 14:34

















          Thank you, but i'm trying to use debugkit but i'm missing smething ; i've enabled it in application.php, but i see nothing on the page, also added code in configuration: book.cakephp.org/3.0/en/debug-kit.html

          – picklerick
          Nov 24 '18 at 11:23







          Thank you, but i'm trying to use debugkit but i'm missing smething ; i've enabled it in application.php, but i see nothing on the page, also added code in configuration: book.cakephp.org/3.0/en/debug-kit.html

          – picklerick
          Nov 24 '18 at 11:23















          Difficult to guess what could be going on. In your config/bootstrap.php do you see this line Plugin::load('DebugKit', ['bootstrap' => true]); ? Is there a debug_kit.sqlite file in the tmp directory?

          – David Albrecht
          Nov 24 '18 at 11:39





          Difficult to guess what could be going on. In your config/bootstrap.php do you see this line Plugin::load('DebugKit', ['bootstrap' => true]); ? Is there a debug_kit.sqlite file in the tmp directory?

          – David Albrecht
          Nov 24 '18 at 11:39













          no, the row is missing in my file.

          – picklerick
          Nov 24 '18 at 11:41





          no, the row is missing in my file.

          – picklerick
          Nov 24 '18 at 11:41













          Solved! seems that you need to have a database called debug_kit

          – picklerick
          Nov 24 '18 at 14:34





          Solved! seems that you need to have a database called debug_kit

          – picklerick
          Nov 24 '18 at 14:34




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456674%2fcakephp3-own-query-builder%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'