[TYPO3-mvc] Left join query

Daniel Dimitrov danielsd_bg at yahoo.fr
Thu Jun 24 11:51:50 CEST 2010


Hey Felix,

"Felix Oertel" <typo3 at foertel.com> wrote in message 
news:mailman.1.1274954152.16041.typo3-project-typo3v4mvc at lists.typo3.org...
> Hey
>
> On 2010-05-27 11:32:55 +0200, Daniel Dimitrov said:
>> Come on guys, I think that this really deserve at least a small 
>> explanation or a nice tutorial :)
>
> ok, small answer: you don't need join queries ;)
>
>> "Daniel Dimitrov" <danielsd_bg at yahoo.fr> wrote in message 
>> news:mailman.1.1274346762.31732.typo3-project-typo3v4mvc at lists.typo3.org...
> I
>>
>>> have a productobject, that can have multiples keywords.
>>> Product object is stored in - tx_bwshop_domain_model_products
>>> I have an mm relation table tx_bwshop_keywords_mm
>>> And the keywords table tx_bwshop_domain_model_keywords
>>>
>>> When I'm showing a list with all products, then extbase make some magic 
>>> tricks and I can see all the keywords for a product :)
>>>
>>> The thing is that I have a search mask where I can search for a keyword 
>>> and I have to show all products for this keyword and its children.
>
> there's a mm-relation between your products and your keywords. first you 
> have to make sure that extbase can follow this relation in both directions 
> (means products have a field "keywords" with MM set and keywords have a 
> field "products" with MM and (!) MM_oposite_field (to "keywords") set [*])
>
> if you want to search for all products containing a keyowrd you just fetch 
> the keyword-model via the keyword-repository (with 
> matching(like($searchphrase)) or similar and then do 
> $keyword->getProducts() as extbase mapps all products containing this 
> keyword-model into that property itself ;-) Why do you want to join 
> yourself? Extbase loves you and takes care for everything *g

Ok, I've managed to do this! Really I didn't have to write any join query - 
exbase did
this for me. However the problem that i have right now is that this
$keyword->getProducts() returns just one row instead of all products for the 
keyword.

My keywords model looks like this:
class Tx_BwShop_Domain_Model_Keywords extends 
Tx_Extbase_DomainObject_AbstractEntity {

	protected $keyword;

	/**
	 *
	 * @var Tx_BwShop_Domain_Model_Products
	 * @lazy
	 */
	protected $products;

	public function getKeyword(){
		return $this->keyword;
	}

	public function getProducts() {
		return $this->products;
	}}

in my controller I'm doing this:
$keyword = $this->keywordsRepository->findByUid($this->filter['kw']);
$products = $keyword->getProducts();

when I make var_dump for products I get this output:
object(Tx_Extbase_Persistence_LazyLoadingProxy)#169 (3) {
  ["parentObject:private"]=>
  object(Tx_BwShop_Domain_Model_Keywords)#126 (5) {
    ["keyword:protected"]=>
    string(6) "Lernen"
    ["products:protected"]=>
    object(Tx_Extbase_Persistence_LazyLoadingProxy)#169 (3) {
      ["parentObject:private"]=>
      object(Tx_BwShop_Domain_Model_Keywords)#126 (5) {
        ["keyword:protected"]=>
        string(6) "Lernen"
        ["products:protected"]=>
        *RECURSION*
        ["_cleanProperties:private"]=>
        array(3) {
          ["keyword"]=>
          string(6) "Lernen"
          ["products"]=>
          object(Tx_Extbase_Persistence_LazyLoadingProxy)#171 (3) {
            ["parentObject:private"]=>
            *RECURSION*
            ["propertyName:private"]=>
            string(8) "products"
            ["fieldValue:private"]=>
            string(1) "7"
          }
          ["uid"]=>
          int(623)
        }
        ["uid:protected"]=>
        int(623)
        ["isClone:private"]=>
        bool(false)
      }
      ["propertyName:private"]=>
      string(8) "products"
      ["fieldValue:private"]=>
      string(1) "7"
    }
    ["_cleanProperties:private"]=>
    array(3) {
      ["keyword"]=>
      string(6) "Lernen"
      ["products"]=>
      object(Tx_Extbase_Persistence_LazyLoadingProxy)#171 (3) {
        ["parentObject:private"]=>
        object(Tx_BwShop_Domain_Model_Keywords)#126 (5) {
          ["keyword:protected"]=>
          string(6) "Lernen"
          ["products:protected"]=>
          object(Tx_Extbase_Persistence_LazyLoadingProxy)#169 (3) {
            ["parentObject:private"]=>
            *RECURSION*
            ["propertyName:private"]=>
            string(8) "products"
            ["fieldValue:private"]=>
            string(1) "7"
          }
          ["_cleanProperties:private"]=>
          array(3) {
            ["keyword"]=>
            string(6) "Lernen"
            ["products"]=>
            object(Tx_Extbase_Persistence_LazyLoadingProxy)#171 (3) {
              ["parentObject:private"]=>
              *RECURSION*
              ["propertyName:private"]=>
              string(8) "products"
              ["fieldValue:private"]=>
              string(1) "7"
            }
            ["uid"]=>
            int(623)
          }
          ["uid:protected"]=>
          int(623)
          ["isClone:private"]=>
          bool(false)
        }
        ["propertyName:private"]=>
        string(8) "products"
        ["fieldValue:private"]=>
        string(1) "7"
      }
      ["uid"]=>
      int(623)
    }
    ["uid:protected"]=>
    int(623)
    ["isClone:private"]=>
    bool(false)
  }
  ["propertyName:private"]=>
  string(8) "products"
  ["fieldValue:private"]=>
  string(1) "7"
}


How can I make $keyword->getProduct() return all the products and not a 
single one.



>
> regards, foertel
>
> PS: if you really want to get the products yourself (which hardly makes 
> sense, cause you have to get the keyword-model upfront anyway) you can do 
> something like products=>matching(contains('keywords', $keywordModel)) ... 
> extbase won't build a join for you but use a subselect in SQL's IN() - the 
> result is the same (at least the resultSET is *g)
>
> [*] if anyone is confused by this I suggest to take a look at all fields 
> starting with MM_ at 
> http://typo3.org/documentation/document-library/core-documentation/doc_core_api/4.1.0/view/4/2/#id4224554
>

Kind Regards,
Daniel 



More information about the TYPO3-project-typo3v4mvc mailing list