This site uses cookies and similar technologies.

If you don't change browser settings, you agree to it.

I understand

Count existing Joomla! Articles

After instantiating the object, you only need to call the count method, by passing:

  • the Content Type you wish to use: 'article', 'blog_post', or any other content type based on the Joomla! Article Object
$content = new JCckContentArticle;
echo $content->count( $content_type ); @ SEBLOD 3.17.0

You've just learned how to count existing Joomla! Articles using JCckContent !

$content = new JCckContentArticle;
$data      = array(
                      'catid'=>2,
                      'state'=>1
                  );

echo $content->count( $content_type, $data ); @ SEBLOD 3.17.0

By passing an array of properties $data (the same way it works for creating a new article) you can alter the search query to filter results, without any specific SQL knowledge. 

$content = new JCckContentArticle;
$data      = array(
                      'catid'=>2,
                      'color'=>'blue',
                      'state'=>1
                  );

echo $content->count( $content_type, $data ); @ SEBLOD 3.17.0

Filtering articles with both standard properties and custom properties is as easy as [eating a] pie.
You just need to complete the $data array with the column names of your custom fields, without specifying to which table instance they actually belong. JCckContent will automatically dispatch the properties across the suitable table instances.

Let's move forward in order to retrieve more than just the total of items.



"Find" vs "Search"

Before exploring the capabilities offered by find and search methods, let's try to define those within our context.

As both allow to query/retrieve existing Content Items, we'll use them as the following:

  • for simple queries, find will work on its own and will be everything you need to identify content items.
  • for more complex queries, you'll need to search first, before actually finding what you are looking for.
    in that case, we'll see the action of finding as the completion of searching

In the background, please note that  find is really performing the SQL query, while search is only preparing (alongside other methods) the query for later on.



Find existing Joomla! Articles

After instantiating the object, in order to query specific articles you need to call the find method first, by passing:

  • the Content Type you wish to use: 'article', 'blog_post', or any other content type based on the Joomla! Article Object
$content = new JCckContentArticle;
$content->find( $content_type ); @ SEBLOD 3.17.0


This will query all existing content items from a specific content type.

$content = new JCckContentArticle;
$data      = array(
                      'catid'=>2,
                      'state'=>1
                  );

$content->find( $content_type, $data ); @ SEBLOD 3.17.0

By passing an array of properties $data (the same way it works for creating a new article) you can alter the search query to filter results, without any specific SQL knowledge. 

$content = new JCckContentArticle;
$data      = array(
                      'catid'=>2,
                      'color'=>'blue',
                      'state'=>1
                  );

$content->find( $content_type, $data ); @ SEBLOD 3.17.0

Filtering articles with both standard properties and custom properties is as easy as [eating a] pie.
You just need to complete the $data array with the column names of your custom fields, without specifying to which table instance they actually belong. JCckContent will automatically dispatch the properties across the suitable table instances.


Until now only the query was performed, but nothing specific was requested in return.

We could choose to find more items or to get the results now.

$data      = array(
                      'catid'=>3,
                      'color'=>'red',
                      'state'=>1
                  );

$content->findMore( $content_type, $data ); @ SEBLOD 3.17.0

Optionally, an additional call using the findMore method would merge its own results to the "results" stack.

Additional note: each call of the find method will clear the "results" stack.

$pks = $content->getPks(); @ SEBLOD 3.17.0

Assuming you have already perform a find (as previously explained), the above is all you need to get an array of all Primary Keys from your results.

$content = new JCckContentArticle;
$data      = array(
                      'catid'=>2,
                      'color'=>'blue',
                      'state'=>1
                  );

foreach ( $content->find( $content_type, $data )->getPks() as $pk ) {
    if ( $content->load( $pk )->isSuccessful() ) {
        // Do something
    }
@ SEBLOD 3.17.0

Here is a complete example where you loop through each result, load each item, and do something.
As JCckContent class supports method chaining, this is quite straightforward and concise.

Additional methods and capabilities will be added later on.



Search for existing Joomla! Articles

As highlighted above, search allows to perform more complex queries, such as:

  • defining a specific matching mode for one of more properties
  • sorting content items with a specific ordering 
$content = new JCckContentArticle;
$content->search( $content_type ); @ SEBLOD 3.17.0

This will prepare the query in order to find existing items from a specific content type, later on.

$content = new JCckContentArticle;
$data      = array(
                      'catid'=>2,
                      'state'=>1
                  );

$content->search( $content_type, $data ); @ SEBLOD 3.17.0

By passing an array of properties $data (both standard properties and custom properties) you can alter the search query to filter future results, without any specific SQL knowledge.

$content->with( 'catid', '!=' ); @ SEBLOD 3.17.0

One call to the with method allows to set the matching mode (by default = is applied) for any defined property (from our previous $data array).

$content->with( 'color', '=', 'blue' ); @ SEBLOD 3.17.0

It can also pass along an additional property.

$content->with( 'created', '>=', '2018-09-01' ); @ SEBLOD 3.17.0

And allows matching with Dates or Numbers, as well.

Available modes are: =, !=, <, <=, >=, >

$content->by( 'title' ); @ SEBLOD 3.17.0

One call to the by method allows to set the ordering.

$content->by( 'created', 'desc' ); @ SEBLOD 3.17.0

The direction can also be defined.


Until now the query was only prepared, but not performed yet, and nothing specific was requested in return. 

$pks = $content->find()->getPks();  

OR

$pks = $content->findPks(); @ SEBLOD 3.17.0

Both calls above are identical, but the 2nd one is the most straightforward.
findPks is an alias of find + getPks, which performs the query and then returns an array of all Primary Keys from your results.

$content = new JCckContentArticle;
$data      = array(
                      'catid'=>2,
                      'color'=>'blue',
                      'state'=>1
                  );

foreach ( $content->search( $content_type, $data )->with( 'color', '!=' )->by( 'title' )->findPks() as $pk ) {
    if ( $content->load( $pk )->isSuccessful() ) {
        // Do something
    }
@ SEBLOD 3.17.0

Here is a complete example using search where you loop through each result, load each item, and do something.
As JCckContent class supports method chaining, this remains quite concise, even if it's a bit less simple than the use of find.

Additional methods and capabilities will be added later on.



Support for other Objects?

Yes! Of course count, find, search are not only supported by JCckContentArticle, but it works the same for:

  • JCckContentArticle
  • JCckContentCategory
  • JCckContentUser
  • JCckContentUserGroup
  • JCckContentUserNote
  • ...
  • JCckContentFree