ManyToOne 关系为每个用户检索数据

ManyToOne relationship retrieve data for each user

我正在尝试开发一种功能,每个连接的用户都可以找到他最喜欢的项目或应用程序的列表,除此之外,他还可以为每个最喜欢的应用程序创建文档或常见问题解答的一对多链接。请在下面找到示例

我成功地做到了每个用户都有自己的应用程序和链接,但链接与他们的应用程序无关:并为所有应用程序复制相同的链接 我找不到问题,我将感谢您的帮助!这是我与 link-APP 关系相关的代码;

Link class

<?php

namespace Carto\cartographieBundle\Entity;

 use Doctrine\ORM\Mapping as ORM;

/**
 * LienDocUserApp
 *
 * @ORM\Table(name="lien_doc_user_app")
 * @ORM\Entity(repositoryClass="Carto\cartographieBundle\Repository\LienDocUserAppRepository")
 */
class LienDocUserApp
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="nomLien", type="string", length=255)
 */
private $nomLien;

/**
 * @var string
 *
 * @ORM\Column(name="lien", type="string", length=255)
 */
private $lien;


/**
 * @ORM\ManyToOne(targetEntity="Application", inversedBy="liensDocUsersApp", cascade={"persist", "merge"})
 * @ORM\JoinColumns({
 *  @ORM\JoinColumn(name="Application_id", referencedColumnName="id")
 * })
 */
private $applications;

/**
 * @ORM\ManyToOne(targetEntity="AuthBundle\Entity\Users", inversedBy="liensDocUsersApp", cascade={"persist", "merge"})
 * @ORM\JoinColumns({
 *  @ORM\JoinColumn(name="User_id", referencedColumnName="id")
 * })
 */
private $fUsers;

申请class

<?php

namespace Carto\cartographieBundle\Entity;

use Carto\cartographieBundle\Repository\ApplicationRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Application
 *
 * @ORM\Table(name="application")
 * @ORM\Entity(repositoryClass="Carto\cartographieBundle\Repository\ApplicationRepository")
 */
class Application
{

public function __toString() {
    
    return $this->nomApp;
    
}

/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="nomApp", type="string", length=255)
 */
private $nomApp;

.....
.....
/**
 * @ORM\OneToMany(targetEntity="LienDocUserApp", mappedBy="applications", cascade={"persist", "remove", "merge"})
 */
private $liensDocUsersApp;

.....

用户类

<?php

namespace AuthBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class Users extends BaseUser
{
/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\ManyToMany(targetEntity="Carto\cartographieBundle\Entity\ProfilMetier", inversedBy="fUsers", cascade={"persist", "merge"})
 * @ORM\JoinTable(name="profilM_fUsers", joinColumns={@ORM\JoinColumn(name="User_id", referencedColumnName="id")},
 * inverseJoinColumns={@ORM\JoinColumn(name="ProfilM_id", referencedColumnName="id")})
 */  
private $profilsMetier;


/**
 * @ORM\ManyToMany(targetEntity="Carto\cartographieBundle\Entity\Application", inversedBy="fUsers", cascade={"persist", "merge"})
 * @ORM\JoinTable(name="App_fUsers", joinColumns={@ORM\JoinColumn(name="User_id", referencedColumnName="id")},
 * inverseJoinColumns={@ORM\JoinColumn(name="App_id", referencedColumnName="id")})
 */
private $applications;


/**
 * @ORM\OneToMany(targetEntity="Carto\cartographieBundle\Entity\LienDocUserApp", mappedBy="fUsers", cascade={"persist", "remove", "merge"})
 */
private $liensDocUsersApp;

public function __construct()
{

    parent::__construct();
    // your own logic
}

控制器

class AccueilController extends Controller {

public function indexAction(Request $request) {
    $em = $this->getDoctrine()->getManager();
    //affichage de toute les application et pour la recherche par profil
    $applications = $em->getRepository('CartoBundle:Application')->findAll();
    //recupération de l'utilisateur connecté
    $user = $this->get('security.token_storage')->getToken()->getUser();
    // récupération lien application et user
    $lienDocUserApp = $em->getRepository('CartoBundle:LienDocUserApp')->findAll();

return $this->render('CartoBundle:Accueil:index.html.twig', array(
                'applications' => $applications,
                 .......
                'user' => $user,
                'form' => $form->createView(),
                'formFile' => $formFile->createView(),
                'lienDocUserApp' => $lienDocUserApp,
                 .......

                
              
    ));
}

Twig 文件

<div class="tab-pane fade" id="profile-kv-3">
                            <table class="table table-striped table-hover" style="width:100%" >
                                <thead>
                                    <tr>
                                        <th>App</th>
                                        <th>Liens</th>
                                        <th>Fichiers</th>
                                    </tr>
                                </thead>
                                <tbody id="myTable">
                                    {%for application in user.applications%}
                                        <tr>
                                            <td style="width:33%">
                                                <div class="avatar-icon">
                                                    <a href="{{application.navigateur}}http: {{application.lien}}" target="blank"> <img src="{{ asset('bundles/carto/images/Applications/'~application.icone~'') }}" ></a>
                                                </div>
                                            </td>
                                            <td style="width:33%">
                                                {%for lien in user.liensDocUsersApp%}
                                                     <li>{{ lien.nomLien }}</li>
                                                {%endfor%}
                                            </td>
                                        </tr>
                                {%endfor%}
                                </tbody>
                            </table>
                        </div>

您只需要为正确的应用程序显示正确的链接的条件

{% for application in user.applications%}
    <tr>
        <td style="width:33%">
            <div class="avatar-icon">
                <a href="{{application.navigateur}}http: {{application.lien}}" target="blank">
                    <img src="{{ asset('bundles/carto/images/Applications/'~application.icone~'') }}"></a>
            </div>
        </td>
        <td style="width:33%">
            {%for lien in user.liensDocUsersApp%}
                {% if lien.applications.id == application.id %}
                    <li>{{ lien.nomLien }}</li>
                {% endif %}
            {% endfor %}
        </td>
    </tr>
{% endfor %}