Search code examples
typo3typo3-7.6.xtypo3-extensions

TYPO3 7.6 Backend module to list values from several tables


I have been struggling for some time now and I can't really find anyone having done the same thing before.

I'm creating a backend module in TYPO3 7.6 which belongs to a shop extension. The shop extension with the backend module was created with the extension builder. The shop has the following three models:

Product (products which can be ordered through the shop)

Productsorder (link to the customer)

ProductsorderPosition (the ordered product, the ordered amount and size and the link to the Productsorder)

The customers are of a model type from a different extension. These customers are linked to fe_users.

Now what I wanna do in my backend module is getting an overview to all these orders listed with the customer, some information about the fe_user and of course the product. I have created a sql-query, which does exactly that:

SELECT p.productname, p.productpriceperpiece,
    pop.amount, pop.size,
    h.name, h.address, h.zipcode, h.city, h.email, h.phone,
    f.first_name, f.last_name, f.email

    FROM `tx_gipdshop_domain_model_productorderposition` AS pop

    JOIN `tx_gipdshop_domain_model_product` AS p ON pop.products = p.uid
    JOIN `tx_gipdshop_domain_model_productsorder` AS po ON pop.productorder = po.uid
    JOIN `tx_gipleasedisturbhotels_domain_model_hotel` AS h ON po.hotel = h.uid
    JOIN `fe_users` AS f ON h.feuser = f.uid

If I use this query from the product repository it gives back the right amount of data records but they're of type product and the products are all "empty" (uid = 0 etc).

I've added an additional action for this in the product controller (getOrdersAction) and in the repository containing the query I've added a method findAllOrders.

I'm still rather a beginner in TYPO3 but I can somehow understand why it returns data sets of type Product when the query is called from the ProductRepository. But what I do not know is how I can get all the information from the query above and list it in the backend module.

I've already thought about moving the query to the ProductsorderPositionRepository but I would probably be faced with a similar problem, it would only return the information from the ProductsorderPosition and everything else would be left out.

Can someone point me to the right direction? Would I need to create another model with separate repository and controller? Isn't there an easier way?

If you need more information, just ask! ;)


Solution

  • First of all, you are doing a joined query with subsets of data mixed from multiple tables. There is nothing against this.

    Because of this, there is no "model" which has the mixed datasets. If you are using the default query thing in a repository, the magic behind the repository assumes that the result of the query statement reflects the defined base model for this repository.

    Moving the query function to another repository does not solve the problem.

    You have not provided the code snippet you are executing the sql statement, so I assume you have used the query thing in the repository to execute the statement. Something like this:

    $result = $query->statement('
    SELECT p.productname, p.productpriceperpiece,
    pop.amount, pop.size,
    h.name, h.address, h.zipcode, h.city, h.email, h.phone,
    f.first_name, f.last_name, f.email
    
    FROM `tx_gipdshop_domain_model_productorderposition` AS pop
    
    JOIN `tx_gipdshop_domain_model_product` AS p ON pop.products = p.uid
    JOIN `tx_gipdshop_domain_model_productsorder` AS po ON pop.productorder = po.uid
    JOIN `tx_gipleasedisturbhotels_domain_model_hotel` AS h ON po.hotel = h.uid
    JOIN `fe_users` AS f ON h.feuser = f.uid', NULL);
    

    or have used the query building stuff.

    First solution

    The first and simpliest solution would be to retrieve the result as plain php array. Before TYPO3 7.0 you could have done this by using this:

    $query->getQuerySettings()->setReturnRawQueryResult(TRUE);
    

    With TYPO3 7.0 this deprecated method was removed from the core. The only way is to define the query and call $query->execute(TRUE); for now.

    This should return the data in pure array form.

    This is the simpliest one, but as we are in the extbase context this should not be suffering enough.

    Second Solution - no, just an idea that I would try next

    The second solution means that you have some work to do and is for now only a suggestion, because I have not tried this by myself.

    1. Create a model with the properties and getter/setters for the result columns of your query
    2. Create a corresponding repository

    Third solution

    Not nice, but if nothing else works, fall back to the old TYPO3 v4 query methods:

    $GLOBALS['TYPO3_DB']->exec_SELECTgetRows([...]))
    

    and replace this with the QueryBuilder in/for TYPO3 v8.

    This is really not nice.

    I hope I could direct you to the right way, even if not giving a full solving solution.