Ad

Setting A Virtual Property Of Model In Repository

- 1 answer

I'ver created an extension for Typo3 to do a radial search for retailers. The user can search for a location and it shows every retailer near that location in a radius from 10 to 50km. Now I want to show the distance of each retailer to the location. Therefore I set up a property in my domain model:

/**
 * distance
 *
 * @var float
 */
protected $distance = '';

/**
 * Returns the distance
 *
 * @return float distance
 */
public function getDistance() {
    return $this->distance;
}

/**
 * Sets the distance
 *
 * @param string $distance
 * @return void
 */
public function setDistance($distance) {
    $this->distance = $distance;
}

The distance is calculated in the repository like this:

$query->statement('SELECT uid,pid,name,address,city, (6371 * acos(cos(radians( '.$lat.' )) * cos(radians( lat )) * cos(radians( lng ) - radians( '.$lng.' )) + sin(radians( '.$lat.' )) * sin(radians( lat )))) AS distance FROM tx_abcretailer_domain_model_retailer HAVING distance <= '.$distance.' ORDER BY distance ASC');
return $query->execute();

And then I want to show it in my fluid template like this:

<f:for each="{retailers}" as="retailer">
    <tr>
        <td class="label">{retailer.name}</td>
        <td>{retailer.address}</td>
        <td>{retailer.zipcode} {retailer.city}</td>
        <td>{retailer.distance} km</td>
    </tr>
</f:for>

But this does not work and I'm a bit lost on how to get the property working and show the distance to the user.

EDIT: Here's also the controller action:

public function searchAction(){
    $arguments  = $this->request->getArguments();

    $context = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Context\Context::class);
    $this->view->assign("lang", $context->getPropertyFromAspect('language', 'id'));
    
    $retailers = $this->retailerRepository->search($arguments);

    $this->view->assign('retailers', $retailers);
    $this->view->assign('arguments', $arguments['searchRetailer']);
}
Ad

Answer

Based on the feedback by Patricks, let's construct a little guide for "future generations", that might stumble on this post.

How to automap a virtual field of a DB query to a TYPO3 model

  1. Add the virtual field to the model with the right type setter/getter
  2. Add the field to the TCA as passthrough
  3. Don't add the field to the DB table itself
  4. In the query add the field with as <name>
Ad
source: stackoverflow.com
Ad