Message from discussion
JMSSerializerBundle + FOSUserBundle with Propel
Received: by 10.58.249.106 with SMTP id yt10mr4631651vec.36.1349709834766;
Mon, 08 Oct 2012 08:23:54 -0700 (PDT)
X-BeenThere: symfony2@googlegroups.com
Received: by 10.220.154.5 with SMTP id m5ls4463192vcw.2.gmail; Mon, 08 Oct
2012 08:23:52 -0700 (PDT)
Received: by 10.52.175.5 with SMTP id bw5mr2541907vdc.16.1349709831983;
Mon, 08 Oct 2012 08:23:51 -0700 (PDT)
Date: Mon, 8 Oct 2012 08:23:51 -0700 (PDT)
From: Nick Winfield <pion...@superhaggis.com>
To: symfony2@googlegroups.com
Message-Id: <13aee4f4-d16f-4d57-9902-2dc01ed7a589@googlegroups.com>
In-Reply-To: <1867954e-e147-49cd-bcf4-66adc65beefc@googlegroups.com>
References: <7def865c-37bc-4fe7-8d33-8e882e4d0dd5@googlegroups.com>
<1867954e-e147-49cd-bcf4-66adc65beefc@googlegroups.com>
Subject: Re: JMSSerializerBundle + FOSUserBundle with Propel
MIME-Version: 1.0
Content-Type: multipart/mixed;
boundary="----=_Part_2190_1320127.1349709831685"
------=_Part_2190_1320127.1349709831685
Content-Type: multipart/alternative;
boundary="----=_Part_2191_7905451.1349709831685"
------=_Part_2191_7905451.1349709831685
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Hello again,
Even though it feels like I'm talking to myself, I thought I'd share my
solution. :P
In order to get things working, however, I had to patch in the following PR
for the metadata package.
https://github.com/schmittjoh/metadata/commit/64ce1d44fd91c27c1426f21e19b8ea9dba268b2f
Basically, I had to exclude all properties accessible via public methods
from the BaseObject class in Propel. The reason I had to patch metadata
was because the BaseObject class, where the unwanted properties reside,
does not have a namespace.
# ./app/config/config.yml
jms_serializer:
metadata:
auto_detection: true
directories:
propel:
namespace_prefix: ""
path: "@FooBarBundle/Resources/config/serializer"
fos:
namespace_prefix: "FOS\\UserBundle"
path: "@FooBarBundle/Resources/config/serializer"
# ./src/Foo/BarBundle/Resources/config/serializer/BaseObject.yml
BaseObject:
exclusion_policy: ALL
I cleared my dev cache and was able to view a trimmed down serialized
representation of the User object.
{"id":1,"username":"foo","email":"f...@bar.com"}
I hope my solution is of help to someone!
Cheers,
Nick.
On Monday, October 8, 2012 2:49:37 PM UTC+1, Nick Winfield wrote:
>
> Hello,
>
> Well, I've made progress. I stuck the serializer configuration into YML
> (pointing at the Propel base class), stuck the file under my main bundle's
> config directory (created a serializer directory) and was able to return a
> filtered, serialized version of the User object.
>
> However, I am getting additional fields coming through despite the
> configuration explicitly set to "expose: false".
>
> # ./app/config/config.yml
> jms_serializer:
> metadata:
> auto_detection: true
> directories:
> fos:
> namespace_prefix: "FOS\\UserBundle"
> path: "@FooBarBundle/Resources/config/serializer"
>
> # ./src/Foo/BarBundle/Resources/config/serializer/Propel.om.BaseUser.yml
> FOS\UserBundle\Propel\om\BaseUser:
> exclusion_policy: ALL
> access_type: public_method
> properties:
> id:
> expose: true
> username:
> expose: true
> email:
> expose: true
> _new:
> expose: false
> _deleted:
> expose: false
> modified_columns:
> expose: false
> virtual_columns:
> expose: false
>
> An example of the JSON being returned:
>
> {"_new":false,"_deleted":false,"modified_columns":[],"virtual_columns":[],"id":1,"username":"foo","email":"f...@bar.com"}
>
>
> I only want the values from the User class, not the meta properties from
> Propel's base classes. Any idea what I'm doing wrong?
>
> Cheers,
> Nick.
>
> On Monday, October 8, 2012 9:59:07 AM UTC+1, Nick Winfield wrote:
>>
>> Hello,
>>
>> Need a little bit of help here, because I seem to be going round in
>> circles. Before I start, I have done a fair amount of research on this
>> issue and have tried the solutions that have been suggested in the
>> JMSSerializerBundle docs as well as other configuration tweaks.
>>
>> Basically, I'm using FOSRestBundle to serve up an API and I chose a
>> simple use case to prove concept before I embark on the actual business
>> logic. I'm trying to spit out JSON representing a filtered version of
>> \FOS\UserBundle\Propel\User but the output always contains a whole bunch of
>> extra fields that should have been omitted via XML configuration.
>>
>> As far as I can tell, I've followed the instructions to the letter so I'm
>> a bit stumped. See code below ("foo" and "bar" swapped in for actual
>> sensitive information)
>>
>> # ./serializer/fos/Propel.User.xml
>> <serializer>
>> <class name="FOS\UserBundle\Propel\User" exclusion-policy="all">
>> <property name="id" type="integer" expose="true"
>> access-type="public_method"></property>
>> <property name="username" type="string" expose="true"
>> access-type="public_method"></property>
>> <property name="usernameCanonical" type="string" expose="true"
>> access-type="public_method"></property>
>> <property name="email" type="string" expose="true"
>> access-type="public_method"></property>
>> <property name="emailCanonical" type="string" expose="true"
>> access-type="public_method"></property>
>> </class>
>> </serializer>
>>
>> # ./app/config/config.yml
>> ...
>>
>> jms_serializer:
>> metadata:
>> directories:
>> FOSUB:
>> namespace_prefix: FOS\UserBundle
>> path: "%kernel.root_dir%/serializer/fos"
>>
>> # ./src/Foo/BarBundle/Controller/UserRestController.php
>> <?php
>>
>> namespace Foo\BarBundle\Controller;
>>
>> use Symfony\Bundle\FrameworkBundle\Controller\Controller;
>>
>> class UserRestController extends Controller
>> {
>> public function getUserAction($userId)
>> {
>> $user = $this->get('security.context')->getToken()->getUser();
>>
>> if ($user) {
>> $view = \FOS\RestBundle\View\View::create($user, 200);
>> } else {
>> $view = \FOS\RestBundle\View\View::create(null, 404);
>> }
>>
>> return $this->get('fos_rest.view_handler')->handle($view);
>> }
>> }
>>
>> And here's the output I'm getting - seems that all the state properties
>> of the Propel object are being included.
>>
>> {"_new":false,"_deleted":false,"modified_columns":[],"virtual_columns":[],"start_copy":false,"id":1,"username":"foo","username_canonical":"foo","email":"f...@bar.com","email_canonical":"f...@bar.com","enabled":true,"salt":"salt","password":"password","last_login":"2012-10-08 09:49:38","locked":false,"expired":false,"credentials_expired":false,"already_in_save":false,"already_in_validation":false,"validation_failures":[]}
>>
>>
>> I looked in the dev cache, and there are a few files with serialized
>> contents corresponding to my configuration.
>>
>> # ./app/cache/dev/jms_serializer/BaseObject.cache.php
>> #
>> ./app/cache/dev/jms_serializer/FOS-UserBundle-Propel-om-BaseUser.cache.php
>> # ./app/cache/dev/jms_serializer/FOS-UserBundle-Propel-User.cache.php
>>
>> Has anyone had any experience using this serializer bundle with Propel
>> objects before? If so, what did you do to get it working?
>>
>> Thanks in advance,
>> Nick.
>>
>
------=_Part_2191_7905451.1349709831685
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Hello again,<br><br>Even though it feels like I'm talking to myself, I thou=
ght I'd share my solution. :P<br><br>In order to get things working, howeve=
r, I had to patch in the following PR for the metadata package.<br><br>http=
s://github.com/schmittjoh/metadata/commit/64ce1d44fd91c27c1426f21e19b8ea9db=
a268b2f<br><br>Basically, I had to exclude all properties accessible via pu=
blic methods from the BaseObject class in Propel. The reason I had to=
patch metadata was because the BaseObject class, where the unwanted proper=
ties reside, does not have a namespace.<br><span style=3D"font-family: cour=
ier new,monospace;"><br># ./app/config/config.yml<br>jms_serializer:<br>&nb=
sp; metadata:<br> aut=
o_detection: true<br> directories=
:<br> pro=
pel:<br> &=
nbsp; namespace_prefix: ""<br> &nb=
sp; path: "@Foo=
BarBundle/Resources/config/serializer"<br> &nb=
sp; fos:<br> &nb=
sp; namespace_prefix:=
"FOS\\UserBundle"<br>  =
; path: "@FooBarBundle/Resources/config=
/serializer"<br><br># ./src/Foo/BarBundle/Resources/config/serializer/BaseO=
bject.yml<br>BaseObject:<br> exclusion_policy: ALL</span>=
<br><br>I cleared my dev cache and was able to view a trimmed down serializ=
ed representation of the User object.<br><br><pre style=3D"color: rgb(0, 0,=
0); font-style: normal; font-variant: normal; font-weight: normal; letter-=
spacing: normal; line-height: normal; orphans: 2; text-align: start; text-i=
ndent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-tex=
t-size-adjust: auto; -webkit-text-stroke-width: 0px; word-wrap: break-word;=
white-space: pre-wrap; ">{"id":1,"username":"foo","email":"f...@bar.com"}</=
pre><br>I hope my solution is of help to someone!<br><br>Cheers,<br>Nick.<b=
r><br>On Monday, October 8, 2012 2:49:37 PM UTC+1, Nick Winfield wrote:<blo=
ckquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-=
left: 1px #ccc solid;padding-left: 1ex;">Hello,<br><br>Well, I've made prog=
ress. I stuck the serializer configuration into YML (pointing at the =
Propel base class), stuck the file under my main bundle's config directory =
(created a serializer directory) and was able to return a filtered, seriali=
zed version of the User object.<br><br>However, I am getting additional fie=
lds coming through despite the configuration explicitly set to "expose: fal=
se".<br><br><span style=3D"font-family:courier new,monospace"># ./app/confi=
g/config.yml<br>jms_serializer:<br> metadata:<br> &n=
bsp; auto_detection: true<br> &nbs=
p; directories:<br> &n=
bsp; fos:<br> &n=
bsp; namespace_prefix=
: "FOS\\UserBundle"<br> &nbs=
p; path: "@FooBarBundle/Resources/<wbr>=
config/serializer"<br></span><br><span style=3D"font-family:courier new,mon=
ospace"># ./src/Foo/BarBundle/Resources/<wbr>config/serializer/Propel.om.<w=
br>BaseUser.yml<br>FOS\UserBundle\Propel\om\<wbr>BaseUser:<br> &=
nbsp; exclusion_policy: ALL<br> access_type: public_metho=
d<br> properties:<br> =
id:<br> &=
nbsp; expose: true<br> username:<=
br> expos=
e: true<br> email:<br>  =
; expose: true<br>&nb=
sp; _new:<br> &n=
bsp; expose: false<br>  =
; _deleted:<br> =
expose: false<br> &nb=
sp; modified_columns:<br> &n=
bsp; expose: false<br>  =
; virtual_columns:<br>  =
; expose: false</span><br><br>An example of the JSO=
N being returned:<br><br><pre style=3D"color:rgb(0,0,0);font-style:normal;f=
ont-variant:normal;font-weight:normal;letter-spacing:normal;line-height:nor=
mal;text-align:start;text-indent:0px;text-transform:none;word-spacing:0px;w=
ord-wrap:break-word;white-space:pre-wrap">{"_new":false,"_deleted":<wbr>fal=
se,"modified_columns":[],"<wbr>virtual_columns":[],"id":1,"<wbr>username":"=
foo","email":"<a href=3D"mailto:f...@bar.com" target=3D"_blank">foo@<wbr>bar=
.com</a>"}</pre><br>I only want the values from the User class, not the met=
a properties from Propel's base classes. Any idea what I'm doing wron=
g?<br><br>Cheers,<br>Nick.<br><br>On Monday, October 8, 2012 9:59:07 AM UTC=
+1, Nick Winfield wrote:<blockquote class=3D"gmail_quote" style=3D"margin:0=
;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br><=
br>Need a little bit of help here, because I seem to be going round in circ=
les. Before I start, I have done a fair amount of research on this is=
sue and have tried the solutions that have been suggested in the JMSSeriali=
zerBundle docs as well as other configuration tweaks.<br><br>Basically, I'm=
using FOSRestBundle to serve up an API and I chose a simple use case to pr=
ove concept before I embark on the actual business logic. I'm trying =
to spit out JSON representing a filtered version of \FOS\UserBundle\Propel\=
User but the output always contains a whole bunch of extra fields that shou=
ld have been omitted via XML configuration.<br><br>As far as I can tell, I'=
ve followed the instructions to the letter so I'm a bit stumped. See =
code below ("foo" and "bar" swapped in for actual sensitive information)<br=
><br># ./serializer/fos/Propel.User.<wbr>xml<br><span style=3D"font-family:=
courier new,monospace"><serializer> <br> <=
class name=3D"FOS\UserBundle\Propel\<wbr>User" exclusion-policy=3D"all">=
<br> <property name=3D"=
id" type=3D"integer" expose=3D"true" access-type=3D"public_method"></=
<wbr>property> <br> <=
property name=3D"username" type=3D"string" expose=3D"true" access-type=3D"p=
ublic_method"></<wbr>property> <br> &=
nbsp; <property name=3D"usernameCanonical" type=3D"string" e=
xpose=3D"true" access-type=3D"public_method"></<wbr>property> =
; <br> <property name=3D"email=
" type=3D"string" expose=3D"true" access-type=3D"public_method"></<wb=
r>property> <br> <pro=
perty name=3D"emailCanonical" type=3D"string" expose=3D"true" access-type=
=3D"public_method"></<wbr>property> <br> &=
lt;/class> <br></serializer></span><br><br># ./app/config/co=
nfig.yml<br><span style=3D"font-family:courier new,monospace">...<br><br>jm=
s_serializer:<br> metadata:<br> &n=
bsp; directories:<br> =
FOSUB:<br> &nbs=
p; namespace_prefix: FOS\Us=
erBundle<br> &nb=
sp; path: "%kernel.root_dir%/serializer/<wbr>fos"</=
span><br><br># ./src/Foo/BarBundle/<wbr>Controller/UserRestController.<wbr>=
php<br><span style=3D"font-family:courier new,monospace"><?php<br><br>na=
mespace Foo\BarBundle\Controller;<br><br>use Symfony\Bundle\<wbr>FrameworkB=
undle\Controller\<wbr>Controller;<br><br>class UserRestController extends C=
ontroller<br>{<br> public function getUserAction($userId)=
<br> {<br> $use=
r =3D $this->get('security.context')<wbr>->getToken()->getUser();<=
br> <br> &=
nbsp; if ($user) {<br>  =
; $view =3D \FOS\RestBundle\View\View::<wbr>create(=
$user, 200);<br> } else {<br>&nbs=
p; $view =3D \F=
OS\RestBundle\View\View::<wbr>create(null, 404);<br>  =
; }<br> <br>&nb=
sp; return $this->get('fos_rest.view=
_<wbr>handler')->handle($view);<br> }<br>}<br><br></sp=
an>And here's the output I'm getting - seems that all the state properties =
of the Propel object are being included.<br><br><pre style=3D"color:rgb(0,0=
,0);font-style:normal;font-variant:normal;font-weight:normal;letter-spacing=
:normal;line-height:normal;text-align:start;text-indent:0px;text-transform:=
none;word-spacing:0px;word-wrap:break-word;white-space:pre-wrap">{"_new":fa=
lse,"_deleted":<wbr>false,"modified_columns":[],"<wbr>virtual_columns":[],"=
start_<wbr>copy":false,"id":1,"username":<wbr>"foo","username_canonical":"<=
wbr>foo","email":"<a href=3D"mailto:f...@bar.com" target=3D"_blank">foo@bar.=
com</a>","<wbr>email_canonical":"<a href=3D"mailto:f...@bar.com" target=3D"_=
blank">f...@bar.com</a>"<wbr>,"enabled":true,"salt":"salt",<wbr>"password":"=
password","last_<wbr>login":"2012-10-08 09:49:38","locked":false,"<wbr>expi=
red":false,"credentials_<wbr>expired":false,"already_in_<wbr>save":false,"a=
lready_in_<wbr>validation":false,"validation_<wbr>failures":[]}</pre><br>I =
looked in the dev cache, and there are a few files with serialized contents=
corresponding to my configuration.<br><br># ./app/cache/dev/jms_<wbr>seria=
lizer/BaseObject.cache.<wbr>php<br># ./app/cache/dev/jms_<wbr>serializer/FO=
S-UserBundle-<wbr>Propel-om-BaseUser.cache.php<br># ./app/cache/dev/jms_<wb=
r>serializer/FOS-UserBundle-<wbr>Propel-User.cache.php<br><br>Has anyone ha=
d any experience using this serializer bundle with Propel objects before? I=
f so, what did you do to get it working?<br><br>Thanks in advance,<br>Nick.=
<br></blockquote></blockquote>
------=_Part_2191_7905451.1349709831685--
------=_Part_2190_1320127.1349709831685--