Joining an undeclared table in Propel or Join Custom Table Propel

The other day, I faced the task of sorting the output of products according to a table that is not described in schema.xml. I will give a brief example of the table schema and table structure with positions, by which I should have sorted the products.

Simplified product table (merged the translation table with the main one and truncated the fields):

id (int) product id
name (string) Name
create_date (int) the date

Schema of this table (schema.xml):

<table name="shop_products" phpName="SProducts">
<vendor type="mysql">
<parameter name="Charset" value="utf8"/>
</vendor>

<column name="id" type="INTEGER" required="true" autoIncrement="true" primaryKey="true"/>
<column name="name" type="varchar" size="500" required="true"/>
<column name="create_date" type="INTEGER"/>
</table>

Let's say we have a binding of products to articles, and each article can have the same product and each product can have its own position in each article.

I will give an example of a table of articles and product links (let's call the table link_page):

page_id (int) Article ID (given as an example)
product_id (int) Product ID from the SProducts schema
position (int) The position in which the product is in the article

Here there is a position for each product in the article, but there is no link in the product table to make join a sing-song. So let's start doing this:

SProductsQuery::create()
->addJoin('shop_products.id' , 'link_page.product_id', Criteria::INNER_JOIN)
->addAsColumn('position', 'link_page.position')
->where('page_link_product.page_id = ' . 'ID страницы товара')
->orderBy('position','ASC')
->find();

In my case, the full path to Criteria::INNER_JOIN is \Propel\Runtime\ActiveQuery\Criteria::INNER_JOIN .

SProductsQuery - the generated model according to the scheme, simplified for understanding and described above.

This query will make it possible to connect a table not declared in the models to the query, so as not to make crutches and bicycles for sorting.

Try it, figure it out, in my case, addJoin() allowed me to save the time of rewriting and regenerating the schema for the needs, thereby not burdening the page of the article, which contains products.

1797 0

Comments

Be the first to review this item. Share your rating and review so that other customers can decide if this is the right item for them.
You have to log in to leave a comment. Sign in

Similar articles

Create and download CSV in PHP

Consider the possibility of quickly creating a CSV file with automatic file download. Consider the formation, separators and header for the ability to download the file.