Symfony Doctrine LAZY Data loading











up vote
0
down vote

favorite












I want to pass some data to JavaScript but i got empty vars like this




var testsCompany = {};



var testparams = "[{}]";




It is some issue related of lazy data loading in doctrine, i tried to change the fetch attribute value in ManyToOne association but in vain.



This is my code:



  <script type="text/javascript">
var testsCompany = "{{ testsCompany|json_encode()|raw}}";
var testparams = "{{ testparams|json_encode()|raw}}";
</script>


This is my entity:



class TestParams
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;

/**
* Many testparams have One test.
* @ORMManyToOne(targetEntity="Tests", inversedBy="testparams",fetch="EAGER")
* @ORMJoinColumn(name="test_id", referencedColumnName="id")
*/
private $test;


/**
* Many testparams have One param.
* @ORMManyToOne(targetEntity="Params", inversedBy="testparams", fetch="EAGER")
* @ORMJoinColumn(name="param_id", referencedColumnName="id")
*/
private $param;

/**
* @var bool
*
* @ORMColumn(name="required", type="boolean")
*/
private $required;

public function __construct()
{
$this->test = new ArrayCollection();
$this->param = new ArrayCollection();
}

public function __toString()
{
return '*';
}

}


and this is the controller code:



/**
* @Route("/generate/{id}", name="prestashoptest_db_generate")
*/
public function generateAction(Request $request)
{
$testId = $request->get('id');
$testRepo = $this->getDoctrine()->getRepository('PrestaShopTestBundle:Tests');
$testParamsRepo = $this->getDoctrine()->getRepository('PrestaShopTestBundle:TestParams');
$testParams = $testParamsRepo->findBy(array("test" => $testId));
$testsCompany = $testRepo->find($testId);

return $this->render('PrestaShopTestBundle:BOTest:generate.html.twig',
array(
"testsCompany" => $testsCompany,
"testParams" => $testParams,
)
);
}









share|improve this question
























  • What about controller? It's the place where you fetch data and pass to view, so it may be helpful.
    – Jakub Matczak
    Nov 7 at 7:58










  • Ok, i will add the method code
    – Mohammad Trabelsi
    Nov 7 at 8:00















up vote
0
down vote

favorite












I want to pass some data to JavaScript but i got empty vars like this




var testsCompany = {};



var testparams = "[{}]";




It is some issue related of lazy data loading in doctrine, i tried to change the fetch attribute value in ManyToOne association but in vain.



This is my code:



  <script type="text/javascript">
var testsCompany = "{{ testsCompany|json_encode()|raw}}";
var testparams = "{{ testparams|json_encode()|raw}}";
</script>


This is my entity:



class TestParams
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;

/**
* Many testparams have One test.
* @ORMManyToOne(targetEntity="Tests", inversedBy="testparams",fetch="EAGER")
* @ORMJoinColumn(name="test_id", referencedColumnName="id")
*/
private $test;


/**
* Many testparams have One param.
* @ORMManyToOne(targetEntity="Params", inversedBy="testparams", fetch="EAGER")
* @ORMJoinColumn(name="param_id", referencedColumnName="id")
*/
private $param;

/**
* @var bool
*
* @ORMColumn(name="required", type="boolean")
*/
private $required;

public function __construct()
{
$this->test = new ArrayCollection();
$this->param = new ArrayCollection();
}

public function __toString()
{
return '*';
}

}


and this is the controller code:



/**
* @Route("/generate/{id}", name="prestashoptest_db_generate")
*/
public function generateAction(Request $request)
{
$testId = $request->get('id');
$testRepo = $this->getDoctrine()->getRepository('PrestaShopTestBundle:Tests');
$testParamsRepo = $this->getDoctrine()->getRepository('PrestaShopTestBundle:TestParams');
$testParams = $testParamsRepo->findBy(array("test" => $testId));
$testsCompany = $testRepo->find($testId);

return $this->render('PrestaShopTestBundle:BOTest:generate.html.twig',
array(
"testsCompany" => $testsCompany,
"testParams" => $testParams,
)
);
}









share|improve this question
























  • What about controller? It's the place where you fetch data and pass to view, so it may be helpful.
    – Jakub Matczak
    Nov 7 at 7:58










  • Ok, i will add the method code
    – Mohammad Trabelsi
    Nov 7 at 8:00













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to pass some data to JavaScript but i got empty vars like this




var testsCompany = {};



var testparams = "[{}]";




It is some issue related of lazy data loading in doctrine, i tried to change the fetch attribute value in ManyToOne association but in vain.



This is my code:



  <script type="text/javascript">
var testsCompany = "{{ testsCompany|json_encode()|raw}}";
var testparams = "{{ testparams|json_encode()|raw}}";
</script>


This is my entity:



class TestParams
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;

/**
* Many testparams have One test.
* @ORMManyToOne(targetEntity="Tests", inversedBy="testparams",fetch="EAGER")
* @ORMJoinColumn(name="test_id", referencedColumnName="id")
*/
private $test;


/**
* Many testparams have One param.
* @ORMManyToOne(targetEntity="Params", inversedBy="testparams", fetch="EAGER")
* @ORMJoinColumn(name="param_id", referencedColumnName="id")
*/
private $param;

/**
* @var bool
*
* @ORMColumn(name="required", type="boolean")
*/
private $required;

public function __construct()
{
$this->test = new ArrayCollection();
$this->param = new ArrayCollection();
}

public function __toString()
{
return '*';
}

}


and this is the controller code:



/**
* @Route("/generate/{id}", name="prestashoptest_db_generate")
*/
public function generateAction(Request $request)
{
$testId = $request->get('id');
$testRepo = $this->getDoctrine()->getRepository('PrestaShopTestBundle:Tests');
$testParamsRepo = $this->getDoctrine()->getRepository('PrestaShopTestBundle:TestParams');
$testParams = $testParamsRepo->findBy(array("test" => $testId));
$testsCompany = $testRepo->find($testId);

return $this->render('PrestaShopTestBundle:BOTest:generate.html.twig',
array(
"testsCompany" => $testsCompany,
"testParams" => $testParams,
)
);
}









share|improve this question















I want to pass some data to JavaScript but i got empty vars like this




var testsCompany = {};



var testparams = "[{}]";




It is some issue related of lazy data loading in doctrine, i tried to change the fetch attribute value in ManyToOne association but in vain.



This is my code:



  <script type="text/javascript">
var testsCompany = "{{ testsCompany|json_encode()|raw}}";
var testparams = "{{ testparams|json_encode()|raw}}";
</script>


This is my entity:



class TestParams
{
/**
* @var int
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;

/**
* Many testparams have One test.
* @ORMManyToOne(targetEntity="Tests", inversedBy="testparams",fetch="EAGER")
* @ORMJoinColumn(name="test_id", referencedColumnName="id")
*/
private $test;


/**
* Many testparams have One param.
* @ORMManyToOne(targetEntity="Params", inversedBy="testparams", fetch="EAGER")
* @ORMJoinColumn(name="param_id", referencedColumnName="id")
*/
private $param;

/**
* @var bool
*
* @ORMColumn(name="required", type="boolean")
*/
private $required;

public function __construct()
{
$this->test = new ArrayCollection();
$this->param = new ArrayCollection();
}

public function __toString()
{
return '*';
}

}


and this is the controller code:



/**
* @Route("/generate/{id}", name="prestashoptest_db_generate")
*/
public function generateAction(Request $request)
{
$testId = $request->get('id');
$testRepo = $this->getDoctrine()->getRepository('PrestaShopTestBundle:Tests');
$testParamsRepo = $this->getDoctrine()->getRepository('PrestaShopTestBundle:TestParams');
$testParams = $testParamsRepo->findBy(array("test" => $testId));
$testsCompany = $testRepo->find($testId);

return $this->render('PrestaShopTestBundle:BOTest:generate.html.twig',
array(
"testsCompany" => $testsCompany,
"testParams" => $testParams,
)
);
}






symfony doctrine2 lazy-loading






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 8:01

























asked Nov 7 at 7:55









Mohammad Trabelsi

702526




702526












  • What about controller? It's the place where you fetch data and pass to view, so it may be helpful.
    – Jakub Matczak
    Nov 7 at 7:58










  • Ok, i will add the method code
    – Mohammad Trabelsi
    Nov 7 at 8:00


















  • What about controller? It's the place where you fetch data and pass to view, so it may be helpful.
    – Jakub Matczak
    Nov 7 at 7:58










  • Ok, i will add the method code
    – Mohammad Trabelsi
    Nov 7 at 8:00
















What about controller? It's the place where you fetch data and pass to view, so it may be helpful.
– Jakub Matczak
Nov 7 at 7:58




What about controller? It's the place where you fetch data and pass to view, so it may be helpful.
– Jakub Matczak
Nov 7 at 7:58












Ok, i will add the method code
– Mohammad Trabelsi
Nov 7 at 8:00




Ok, i will add the method code
– Mohammad Trabelsi
Nov 7 at 8:00












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Finally i have a solution:





  1. I installed a serializer:



    composer require jms/serializer-bundle



  2. configure it :



After you have installed the package, you just need to add the bundle to your AppKernel.php file:



new JMSSerializerBundleJMSSerializerBundle(),




  1. and combine twig helpers:



 <script type="text/javascript">
var testsCompany = {% autoescape %}{{ testsCompany|serialize|raw }}{% endautoescape %};
var testParams = {% autoescape %}{{ testParams|serialize|raw }}{% endautoescape %};
</script>






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185411%2fsymfony-doctrine-lazy-data-loading%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    Finally i have a solution:





    1. I installed a serializer:



      composer require jms/serializer-bundle



    2. configure it :



    After you have installed the package, you just need to add the bundle to your AppKernel.php file:



    new JMSSerializerBundleJMSSerializerBundle(),




    1. and combine twig helpers:



     <script type="text/javascript">
    var testsCompany = {% autoescape %}{{ testsCompany|serialize|raw }}{% endautoescape %};
    var testParams = {% autoescape %}{{ testParams|serialize|raw }}{% endautoescape %};
    </script>






    share|improve this answer

























      up vote
      0
      down vote



      accepted










      Finally i have a solution:





      1. I installed a serializer:



        composer require jms/serializer-bundle



      2. configure it :



      After you have installed the package, you just need to add the bundle to your AppKernel.php file:



      new JMSSerializerBundleJMSSerializerBundle(),




      1. and combine twig helpers:



       <script type="text/javascript">
      var testsCompany = {% autoescape %}{{ testsCompany|serialize|raw }}{% endautoescape %};
      var testParams = {% autoescape %}{{ testParams|serialize|raw }}{% endautoescape %};
      </script>






      share|improve this answer























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        Finally i have a solution:





        1. I installed a serializer:



          composer require jms/serializer-bundle



        2. configure it :



        After you have installed the package, you just need to add the bundle to your AppKernel.php file:



        new JMSSerializerBundleJMSSerializerBundle(),




        1. and combine twig helpers:



         <script type="text/javascript">
        var testsCompany = {% autoescape %}{{ testsCompany|serialize|raw }}{% endautoescape %};
        var testParams = {% autoescape %}{{ testParams|serialize|raw }}{% endautoescape %};
        </script>






        share|improve this answer












        Finally i have a solution:





        1. I installed a serializer:



          composer require jms/serializer-bundle



        2. configure it :



        After you have installed the package, you just need to add the bundle to your AppKernel.php file:



        new JMSSerializerBundleJMSSerializerBundle(),




        1. and combine twig helpers:



         <script type="text/javascript">
        var testsCompany = {% autoescape %}{{ testsCompany|serialize|raw }}{% endautoescape %};
        var testParams = {% autoescape %}{{ testParams|serialize|raw }}{% endautoescape %};
        </script>







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 7 at 9:51









        Mohammad Trabelsi

        702526




        702526






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185411%2fsymfony-doctrine-lazy-data-loading%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings