Jump to content

count() and find() difference help


jarek
 Share

Recommended Posts

Hello! I am new here.

As far as I understand from the documentation, $pages->find("selector") and $pages->count("selector") should work in a similar way. Why does count() in the following code counts 3 items, while find() returns only one?

echo ($pages->get(1021)->Artist->get("Artist_firstname=" . $firstname . ", Artist_lastname=" . $lastname)->Artist_works->count("Artist_works_inventory_no='1'")); //returns 3
echo ($pages->get(1021)->Artist->get("Artist_firstname=" . $firstname . ", Artist_lastname=" . $lastname)->Artist_works->find("Artist_works_inventory_no='1'")); //returns 1069 that is ID of the correct item

 

On the page 1021 there is Artist repeater field and each has Artist_works repeater inside. For the artist with my firstname and lastname there are 3 works in total but only one has attribute Artist_works_inventory_no = 1.

Link to comment
Share on other sites

Why does count matches 3 pages if only one item has attribute Artist_works_inventory_no='1'?

I get the same count number no matter what selector I use:

echo ($pages->get(1021)->Artist->get("Artist_firstname=" . $firstname . ", Artist_lastname=" . $lastname)->Artist_works->count("wrong_name=122")); // also returns 3

 

Link to comment
Share on other sites

I think the count in your case returns the number of children of the page returned by

$pages->get(1021)->Artist->get("Artist_firstname=" . $firstname . ", Artist_lastname=" . $lastname)->Artist_works

Do you really need so much method chaining? You can probably get the page you need with just one selector.

Link to comment
Share on other sites

Right, I am trying this:

echo ($pages->count("Artist.Artist_works.Artist_works_inventory_no=1, Artist.Artist_firstname=" . $firstname . ", Artist.Artist_lastname=" . $lastname));
echo ($pages->count("Artist.Artist_works.Artist_works_inventory_no=1"));

and both return 1. Unfortunately when I set Artist_works_inventory_no=1 on more than one Artist_works (of the same Artist), it still counts only 1. I do not get it at all :(
 

Link to comment
Share on other sites

20 hours ago, fbg13 said:

I think the count in your case returns the number of children of the page returned by

I think so too.

If the inventory number is unique for all art work, you could use:

echo $pages->count("template=repeater_Artist_works,Artist_works_inventory_no=1");

The repeater before Artist_works comes from PW adding before storing the repeater field in Admin/Repeaters

Link to comment
Share on other sites

 

14 hours ago, jarek said:

Unfortunately when I set Artist_works_inventory_no=1 on more than one Artist_works (of the same Artist), it still counts only 1. I do not get it at all :(

The repeaters are nested so you'll need to approach this differently. In your page tree, you'll see that the repeaters are not true pages of your Artist page. If you look at settings of the repeater fields you created, you'll see they have templates and parents that belong in then Admin/Repeaters area. If you must use repeaters like this (which I don't think you do) you'll need to target the repeater templates/parents. Since your attempt shows that Artist_works_inventory_no is not unique, you could try:

// get ID of repeater with artist that matches the first/last name provided
$artistRepeaterID = $pages->get("template=repeater_Artist,Artist_firstname={$firstname}, Artist_lastname={$lastname}")->id;
// repeaters have names like for-page-someID, check your page tree to see what I mean
$artistWorkParent = "for-page-".$artistRepeaterID;
// Find the matching children that have the inventory id and count
echo $pages->get("name={$artistWorkParent}")->children("Artist_works_inventory_no=1")->count();

Personally, I think this approach with nested repeaters could be reevaluated. Many you can explain what you're trying to accomplish?

Link to comment
Share on other sites

There is an Artists page (regular PW page). On this page there are multiple artists and each artist has multiple works. Each work has an inventory number property that sometimes may not be unique. Works also have images, titles etc. So it looks like this:

Artists (page) -> Artist (repeater) -> Work (repeater) -> inventory (string)

I don't see any other approach that would be easy to maintain and edit on admin page.

@MindFull , your code counts other repeater (Biography) that is another child of Artist repeater.

 

Link to comment
Share on other sites

Just an idea: use PageTable. Maybe use a template for artist that has Artist_firstname and Artist_lastname, etc. Then, a pageTable called Artist_pageTable that uses the artist template. Also, create  a template called Artist_works with the associated work fields, including the Artist_works_inventory_no. Then create a pageTable called Artist_works_pageTable that uses the Artist_works template.  Since everything's a page, you could do this:

$pages->get("template=artist,Artist_firstname=$firstname,Artist_lastname=$lastname")->children("Artist_works_inventory_no=1")->count();

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...