我正在开发游戏应用程序并使用Symfony 2.0.我对后端有很多AJAX请求.更多的响应是将实体转换为JSON.例如:
class DefaultController extends Controller
{
public function launchAction()
{
$user = $this->getDoctrine()
->getRepository('UserBundle:User')
->find($id);
// encode user to json format
$userDataAsJson = $this->encodeUserDataToJson($user);
return array(
'userDataAsJson' => $userDataAsJson
);
}
private function encodeUserDataToJson(User $user)
{
$userData = array(
'id' => $user->getId(),
'profile' => array(
'nickname' => $user->getProfile()->getNickname()
)
);
$jsonEncoder = new JsonEncoder();
return $jsonEncoder->encode($userData, $format = 'json');
}
}
Run Code Online (Sandbox Code Playgroud)
我的所有控制器都做同样的事情:获取一个实体并将其一些字段编码为JSON.我知道我可以使用规范化器并对所有权限进行编码.但是,如果一个实体已经循环链接到其他实体呢?或实体图非常大?你有什么建议吗?
我想一下实体的一些编码模式......或者NormalizableInterface
用来避免循环..,
我正在使用symfony2和SonataAdminBundle.我有一个名为Post的简单实体,其中我的内容字段基本上是html文本(来自记录的ckeditor).我需要在Post列表中将内容字段显示为原始html,而不是转义它.像这样黑客攻击base_list_field模板
{% block field %}{{ value|raw }}{% endblock %}
Run Code Online (Sandbox Code Playgroud)
有效,但显然不是正确的方法.任何提示?
编辑:已解决!我在config.yml中为sonata_doctrine_orm_admin定义了一个自定义html类型:
sonata_doctrine_orm_admin:
templates:
types:
list:
html: MyBundle:Default:list_html.html.twig
Run Code Online (Sandbox Code Playgroud)
并创建了自定义list_html.html.twig模板,其中我不转义HTML:
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field%}
{{value|raw}}
{% endblock %}
Run Code Online (Sandbox Code Playgroud)
现在,在PostAdmin中,我可以在configureListFields方法中定义字段的行为:
$listMapper
->add('content', 'html')
Run Code Online (Sandbox Code Playgroud) 我正在使用WordPress.我有一个像这样的图像文件夹mytheme/images/myimages
.
我想从文件夹中检索所有图像名称 myimages
请指教,如何获取图片名称.
我正在尝试找到一个在新创建的窗口上设置默认窗口大小的地方.
在iterm1中有显示配置文件,我没有看到iterm2. http://iterm.sourceforge.net/usage.shtml
我必须使用python计算文本中的单词频率.我想把字词保存在字典中并对每个单词进行计数.
现在,如果我必须根据出现次数对单词进行排序.我可以使用相同的字典而不是使用新的字典,其中键作为计数和单词数组作为值吗?
我有一个商家实体,其中包含以下字段和关联: -
/**
* @ORM\ManyToMany(targetEntity="Category", inversedBy="merchants")
*/
public $categories;
/**
* @ORM\ManyToMany(targetEntity="Tag", inversedBy="merchants")
*/
public $tags;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="merchants")
*/
protected $primaryCategory;
/**
* @ORM\ManyToOne(targetEntity="Tag", inversedBy="merchants")
*/
protected $primaryTag;
Run Code Online (Sandbox Code Playgroud)
标签和类别也有ManyToMany映射.所以我们有Tag_Category,Merchant_Tag,Merchant_Category映射表.
现在我想在这些字段上执行一些ajax.
我想让用户先选择主标签.在主标记的基础上,ajax将类别刷新为仅属于此标记的类别以及更多操作.
我怎样才能做到这一点?
谢谢!
我在erb中有一个编辑表单.
<%= form_for @animal do |f| %>
Run Code Online (Sandbox Code Playgroud)
在代码中我有一个选项选项:
<%= f.select :gender, options_for_select([['Mare'], ['Stallion'], ['Gelding']], :selected => :gender) %>
Run Code Online (Sandbox Code Playgroud)
但是,选择未显示正确的选定值.我能做错什么?如果我对它进行硬编码,我可以让它工作,但当然这不是一个可行的选择.
有没有办法让typescript编译器搜索给定目录中的声明文件?类似于C++的INCLUDE_PATH.
我知道这个问题经过了很多研究,但这里的解决方案并没有解决它们.
让我们从一些背景信息开始:
OS X 10.8.4 Apache 2.2.22
问题:我在控制台中遇到此错误,Apache无法找到我的localhost,但确实启动了.奇怪的.
[Sat Aug 17 13:40:06 2013] [info] mod_ssl/2.2.22 compiled against Server: Apache/2.2.22, Library: OpenSSL/0.9.8r
httpd: Could not reliably determine the server's fully qualified domain name, using Specter.local for ServerName
Run Code Online (Sandbox Code Playgroud)
所以通常这会指向我的ServerName没有设置正确.它是:/我尝试过不同的变种,如Specter.local,localhost等
这是我的/private/etc/httpd.conf的副本,这与/private/etc/apache2/httpd.conf相同
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a …
Run Code Online (Sandbox Code Playgroud) 在这个页面上,我找到了如何为自定义操作添加路由.
protected function configureRoutes(RouteCollection $collection) {
$collection->add('ispremium', $this->getRouterIdParameter().'/ispremium');
}
Run Code Online (Sandbox Code Playgroud)
之后,我在Admin类中添加自定义操作:
protected function configureListFields(ListMapper $listMapper)
{
$listMapper
->addIdentifier('id')
->add('code', null, array('label' => 'Code'))
->add('_action', 'actions', array(
'actions' => array(
'ispremium' => array(
'template' => 'AppMyBundleBundle:Admin:ispremium.html.twig'
)
)
))
;
}
Run Code Online (Sandbox Code Playgroud)
它生成的URL如下:
/app_dev.php/admin/mobispot/discodes/discode/300876/ispremium
Run Code Online (Sandbox Code Playgroud)
我的链接模板:
<a href="{{ admin.generateObjectUrl('ispremium', object) }}">Link</a>
Run Code Online (Sandbox Code Playgroud)
我不知道如何解决这个问题:
如何为该路由传递定义自定义控制器?现在我有一个错误:
方法"Sonata\AdminBundle\Controller\CRUDController :: ispremiumAction"不存在.
我可以使用generateUrl方法更改生成的网址吗?
symfony ×4
sonata-admin ×3
ajax ×2
php ×2
apache ×1
doctrine ×1
doctrine-orm ×1
form-helpers ×1
get ×1
image ×1
iterm ×1
macos ×1
python ×1
ruby ×1
selected ×1
sorting ×1
twig ×1
typescript ×1
web ×1
wordpress ×1