HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Get similar posts with manyToMany relationship

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
manytomanywithgetpostssimilarrelationship

Problem

I have a table post with a many-to-many relationship with the tag and product tables.

  • post \$\rightarrow\$ tag



  • post \$\rightarrow\$ product



How can I make this faster?

For example, if I have post, I can get similar posts similar by tags and products:

Post.php

```
tag = new ArrayCollection();
$this->product = new ArrayCollection();
$this->category = new ArrayCollection();
$this->agency = new ArrayCollection();
}

/**
* Set title
*
* @param string $title
* @return Post
*/
public function setTitle($title)
{
$this->title = $title;

return $this;
}

/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}

/**
* Set content
*
* @param string $content
* @return Post
*/
public function setContent($content)
{
$this->content = $content;

return $this;
}

/**
* Get content
*
* @return string
*/
public function getContent()
{
return $this->content;
}

/**
* Set enabled
*
* @param boolean $enabled
* @return Post
*/
public function setEnabled($enabled)
{
$this->enabled = $enabled;

return $this;
}

/**
* Get enabled
*
* @return boolean
*/
public function getEnabled()
{
return $this->enabled;
}

/**
* Set keepOnTop
*
* @param boolean $keepOnTop
* @return Post
*/
public function setKeepOnTop($keepOnTop)
{
$this->keepOnTop = $keepOnTop;

return $this;
}

/**
* Get keepOnTop
*
* @return boolean
*/
public function getKeepOnTop()
{
return $this->keepOnTop;
}

/**
* Set published
*
* @param \DateTime $published
* @return Post
*/
public function setPublished($published)

Solution

In Post, most setters have a fluent interface,
returning $this, that allow chained calls.

However, you break the consistency with a few setters,
for example setEnabledAfterCreate.
This inconsistency can be confusing,
as users of the class will have to reference the implementation to know which setters can be chained and which cannot.

Similarly,
it's odd that closely related operations like addTag and removeTag,
don't have a symmetric implementation:
addTag returns $this but removeTag returns nothing.
The same goes for the corresponding methods for product, category, agency.


How can I make this faster?

I cannot see anything.
The conditions you have seem all necessary.
They are on foreign keys, so they must be already indexed.
They are != conditions, no LIKE.
I don't see how this can get any faster.

Context

StackExchange Code Review Q#105373, answer score: 3

Revisions (0)

No revisions yet.