Problems, need help? Have a tip or advice? Post it here.
14 posts Page 1 of 2
Hello,

I have already talked about how to use Relation fields with DB_Persist, and it worked fine.
But now I have to use the CSV importer and get data from a field inside the CSV, that is linked to what should be selected inside a relation field on the actual page.

I must find a way to do this.

What I have tried is simply this :

Code: Select all
                    <cms:db_persist
                        _auto_title       = '1'
                        _invalidate_cache = '0'
                        _masterpage       = 'contrats.php'
                        _mode             = 'create'

                        contrat_status  = _contrat_status
                        block_evenement = my_repeatable
                        my_uid          = _my_uid
                        contact_dropdown= _contact_dropdown

                       
                    >


(contact_dropdown being the relation field).

It's not working as you might of guess. Any idea how I can make this work?
Really important that I find a way.

Thanks a lot!
Hi,

To set a relation field via <cms:db_persist>, we need to provide the page's ID (as an example, take a look at this - viewtopic.php?f=2&t=11576#p30955).

In your case, since the data is coming from the CSV, it is likely to be a name. So, you need to first 'pre-process' that name to convert it into a Couch page id.

To that you can feed the name into <cms:pages>, fetch the page with that name from the right masterpage and get hold of 'k_page_id' variable.
It is this value that you should later feed into <cms:db_persist>.

Hope this helps.
Hello KK,

Thanks for the tips.

What I have tried is :

Code: Select all
<cms:set get_contact="<cms:pages masterpage="contrats.php" custom_field="my_uid=_my_uid"><cms:related_pages 'contact_dropdown' ids_only='1' /></cms:pages>"/>


Code: Select all
<cms:db_persist
                        _auto_title       = '1'
                        _invalidate_cache = '0'
                        _masterpage       = 'contrats.php'
                        _mode             = 'create'

                        contrat_status  = _contrat_status
                        block_evenement = my_repeatable
                        my_uid          = _my_uid
                        contact_dropdown= get_contact

                       
                    >


There's a column in the CSV called "my_uid", and also a field in the "contrats" page with the same name. There's a unique number in that field for each "contrats" pages.

I'm not sure about my "custom_field" parameter. I used "_my_uid" thinking it would of get the data from each rows of that CSV file, getting the "my_uid" content. But it's not working.

Do you know what I must change to make it work?
The correct syntax is -
Code: Select all
custom_field="my_uid=<cms:show _my_uid />"

Hope this helps.
I can't make it work. Here's what I have including the masterpage block :

Code: Select all
<cms:set get_contact="<cms:pages masterpage='contrats.php' custom_field="my_uid=<cms:show _my_uid />"><cms:related_pages 'contact_dropdown' ids_only='1' /></cms:pages>" />
                       
                    <cms:db_persist
                        _auto_title       = '1'
                        _invalidate_cache = '0'
                        _masterpage       = 'contrats.php'
                        _mode             = 'create'

                        contact_dropdown= get_contact
                        contrat_status  = _contrat_status
                        block_evenement = my_repeatable
                        my_uid          = _my_uid
                        k_publish_date     = _k_page_date
                        ...


I also tried with
Code: Select all
contact_dropdown= "<cms:show get_contact/>"


instead, but not working either.
Any ideas? Thanks
Work in small steps -
forget the cms:db_persist for a while and try out the other piece of your code separately.
For example, pick and use a new '_my_uid' from the csv and try out as follows -
Code: Select all
<cms:set _my_uid='324' />
<cms:pages masterpage='contrats.php' custom_field="my_uid=<cms:show _my_uid />">
    <cms:dump />
</cms:pages>

Does it fetch the correct contract?

If it does, next introduce the relation within it -
Code: Select all
<cms:set _my_uid='324' />
<cms:pages masterpage='contrats.php' custom_field="my_uid=<cms:show _my_uid />">
    <cms:related_pages 'contact_dropdown' />
       <cms:dump />
   </cms:related_pages>
</cms:pages>

Does it fetch the related page you are expecting?

My guess is that your logic is flawed somewhere - you are trying to query a page from contract.php and later on trying to create a new page using that data; that page wouldn't even exist when you query it in the first place.

Please rethink what your logic should be.
'contact_dropdown' is related to which template? You need to query that template directly (using some data from the csv) for the page that will finally be fed into csv:db_persist to relate it to the contract page being created.

I don;t have sufficient data to give you exact steps - you'll have to figure it out yourself. I am sure you'll manage it.

Hope this helps.
Hi KK,

Thanks for the help.

So I have managed to get the data with the code you have posted.

What I am having difficulty with is mostly how to use data from inside the CSV in my requests.

This :

Code: Select all
<cms:set _my_uid='35814' />
<cms:pages masterpage="contrats.php" custom_field="my_uid=<cms:show _my_uid />"><cms:related_pages "contact_dropdown" ids_only="1" /></cms:pages>


Works perfectly.

But I don't know how to use it in the db_persist part.

I tried this :

Code: Select all
                    <cms:db_persist
                        _auto_title       = '1'
                        _invalidate_cache = '0'
                        _masterpage       = 'contrats.php'
                        _mode             = 'create'

                        contrat_status  = _contrat_status
                        block_evenement = my_repeatable
                        my_uid          = _my_uid
                        k_publish_date     = _k_page_date
                        contact_dropdown= '<cms:pages masterpage="contrats.php" custom_field="my_uid=<cms:show _my_uid />"><cms:related_pages "contact_dropdown" ids_only="1" /></cms:pages>'
                     
                    >


And it's not working. What am I missing here?
But I don't know how to use it in the db_persist part.

The easiest way would be to 'capture' your code's output into a single variable e.g. as follows -
Code: Select all
<cms:capture into='my_contact'>
<cms:set _my_uid='35814' />
<cms:pages masterpage="contrats.php" custom_field="my_uid=<cms:show _my_uid />"><cms:related_pages "contact_dropdown" ids_only="1" /></cms:pages>
</cms:capture>

Now you can use that variable ('my_contact' in the example above) with cms:db_persist e.g.
Code: Select all
..
k_publish_date     = _k_page_date
contact_dropdown=  my_contact

That said, I still feel there is some flaw in your logic (not code).
Try using your code and, in case, it doesn't work the way you expect, please PM me the FTP/Couch creds of your site.
I'll take a look at how the two templates are related and try fo work out a solution (I'll also need some real-world CSV data that is not yet imported).
I think we are not understanding each other correctly.
Here's a graphic to display what I can't manage to do. Maybe that will help! Thanks
Image
Thanks. The graphic confirms my apprehension that you were following the wrong tack -
you were trying to get the ID of a page from contact.php (e.g. 'Microsoft Lab') by first querying for a page from 'contrats.php' that is related to it.

The problem with this was that the contrats page with an ID equal to 'my_uid' will not exist at the point you are trying to query it (you will be creating it with cms:db_persist *after* your futile query we are discussing).

The solution is straight forward - directly query the 'contact.php' for a cloned page titled 'Microsoft Lab' e.g. as follows -
Code: Select all
<cms:pages masterpage='contact.php' page_title='Microsoft Lab' limit='1'>
    ... all variables of the contact here ..
</cms:pages>

Of course, in your case the name would be coming from the CSV, so instead of a hardcoded page title, the code above would have the relevant variable fetched from the CSV (I am assuming below the variable is '_contact_dropdown') -
Code: Select all
<cms:pages masterpage='contact.php' page_title=_contact_dropdown limit='1'>
    ...
</cms:pages>

Hope this helps you in managing to get the sought contact's ID.
14 posts Page 1 of 2