我尝试将验证器注入我的服务 - 但我找不到它:
mybundle.service.supplier:
class: AppBundle\Service\SupplierService
calls:
- [setValidator, ['@validator']]
Run Code Online (Sandbox Code Playgroud)
@validator不是预期的RecursiveValidator http://api.symfony.com/3.1/Symfony/Component/Validator/Validator/RecursiveValidator.html - @validator是一个接口.
那么如何将验证器注入我的服务?
这就是我要的:
<?php
namespace AppBundle\Service;
use AppBundle\Entity\Supplier;
use AppBundle\Helper\EntityManagerTrait;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Validator\Validator\RecursiveValidator;
/**
* Class SupplierService
* @package AppBundle\Service
*/
class SupplierService
{
use EntityManagerTrait;
/** @var RecursiveValidator $validator */
protected $validator;
/**
* @return RecursiveValidator
*/
public function getValidator()
{
return $this->validator;
}
/**
* @param RecursiveValidator $validator
* @return SupplierService
*/
public function setValidator($validator)
{
$this->validator = $validator;
return $this;
}
public function …Run Code Online (Sandbox Code Playgroud) 我有一个带有帖子数据的API调用; 让我们说这是登录过程.
使用Chrome的Postman扩展程序,我通过POST发送用户名和密码来登录用户.但是我收到了以下消息:
Illuminate \ Session \ TokenMismatchException
Run Code Online (Sandbox Code Playgroud)
在我的基本控制器中,我有:
/**
* Initializer.
*
* @return void
*/
public function __construct() {
// CSRF Protection
$this->beforeFilter('csrf', array('on' => 'post'));
// Layouts/Notifications
$this->messageBag = new Illuminate\Support\MessageBag;
}
Run Code Online (Sandbox Code Playgroud)
当我用beforeFilter删除行时,一切正常.但这不是一个解决方案.任何POST调用都会收到此错误消息.我知道我需要这个_token.但是当我从API调用时,我如何获得此令牌?我知道我可以在Laravel中创建一个令牌,但是当我通过API从外部打电话时我怎么能这样做呢?
我是Symfony 3中的新手,我想避免控制器中的业务逻辑.到目前为止我所做的是:
<?php
namespace RestBundle\Controller;
use RestBundle\Entity\Attribute;
use RestBundle\Entity\DistributorProduct;
use RestBundle\Entity\AttributeValue;
use RestBundle\Entity\ProductToImage;
use Symfony\Component\HttpFoundation\Request;
use RestBundle\Entity\Product;
use FOS\RestBundle\Controller\FOSRestController;
/**
* Product controller.
*
*/
class ProductController extends FOSRestController
{
/**
* Creates a new Product entity.
*
*/
public function createProductAction(Request $request)
{
// Doctrine Manager
$em = $this->getDoctrine()->getManager();
// todo: get the logged in distributor object
$distributor = $em->getRepository('RestBundle:Distributor')->find(1);
// Main Product
$product = new Product();
$product->setEan($request->get('ean'));
$product->setAsin($request->get('asin'));
$em->persist($product);
// New Distributor Product
$distributorProduct = new DistributorProduct();
$distributorProduct->setDTitle($request->get('title')); …Run Code Online (Sandbox Code Playgroud) 如何通过新进程在服务中运行命令(app/console execute:my:command)?
我试试这个:
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
$process = new Process(
'app/console execute:my:command'
);
$process->start();
Run Code Online (Sandbox Code Playgroud)
但没有任何反应......如果我通过终端手动调用它,它可以工作:
app/console execute:my:command
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
编辑 - 解决方案:我们需要编写整个路径.就我而言:
Run Code Online (Sandbox Code Playgroud)($this->kernelRootDir is : %kernel.root_dir%)
$processString = sprintf(
'php %s/../app/console %s %s',
$this->kernelRootDir,
self::MY_COMMAND,
$myArgument
);
$process = new Process($processString);
$process->start();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
Run Code Online (Sandbox Code Playgroud) 我有两个型号:
用户和 资源
关系表是resource_user.
resource_user中的字段是:
id | resource_id | user_id | another_id
我在用户中有这种关系:
public function resources() {
return $this->belongsToMany('Resource')->withPivot(array(
'value',
'another_id',
));
}
Run Code Online (Sandbox Code Playgroud)
现在我要更新我的数据透视表:
(在模型用户中有这个代码示例)
$this->resources()->whereAnotherId(1)->updateExistingPivot($resource_id, array(
'value' => $value,
'updated_at' => new DateTime,
));
Run Code Online (Sandbox Code Playgroud)
问题是another_id.
如果我的关系表(resource_user)中有两个条目但是具有不同的 another_id.在此示例中,laravel将更新BOTH条目.但这不是我想要的.在此示例中,只应更新一个条目(具有another_id = 1的条目).这是一个错误,或者我如何更新我的数据透视表(sync()函数不能用于我的表设置)
我有一个users表,其中有一个名为 的整数字段ep。
现在,我想检索按 EP 字段排序的特定组中的所有用户,并使用生成的名为 的 MySQL 字段rank。
这是我尝试过的:
DB::transaction(function()
{
DB::statement("SET @rank:=0");
$group = Sentry::findGroupByName('Player');
$users = Sentry::getUserProvider()->createModel()
->join('throttle', 'throttle.user_id', '=', 'users.id')
->join('users_groups', 'users_groups.user_id', '=', 'users.id')
->where('throttle.banned', 0)
->where('throttle.suspended', 0)
->where('users_groups.group_id', $group->id)
->orderBy('ep', 'desc')
->paginate(100, array('@rank := @rank + 1', 'users.ep', 'users.username'));
});
Run Code Online (Sandbox Code Playgroud)
但我收到了这个错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '@rank := @rank + 1' in 'field list' (SQL: select `@rank := @rank + 1`, `users`.`ep`, `users`.`username` from `users` inner join `throttle` …Run Code Online (Sandbox Code Playgroud) 使用存储库我得到了一个数组结果(每个数组都是一个实体对象),如下所示:
array(
0 => object of type entity,
1 => another object of type entity,
2 => another object of type entity,
)
Run Code Online (Sandbox Code Playgroud)
每个对象都有一些属性,如id和name等.但我想要的只是用每个对象的id展平整个数组.
我想要的是这个(仅使用ID's flatten the array):
Array
(
[0] => 1
[1] => 6
[2] => 23
)
Run Code Online (Sandbox Code Playgroud)
我的解决方案
$ids = array_map($transform = function($entity) {
if ($entity instanceof Entity) {
return $entity->getId();
}
}, $myGreatDbResult);
Run Code Online (Sandbox Code Playgroud)
我的解决方案正在运行,但是有更好的方法来获得这个结果吗?
我有一条路线:
Route::group(array('prefix' => 'playlist'), function()
{
Route::get('raw/{id}', array('as'=>'rawPlaylist', 'uses'=>'PlaylistsController@raw'));
});
Run Code Online (Sandbox Code Playgroud)
当我打开URL时:
http://localhost:8000/playlist/raw/1
Run Code Online (Sandbox Code Playgroud)
一切都很好,页面将加载并显示视图.
在控制器中,我得到了URL:
$url = URL::route('rawPlaylist', 1);
Run Code Online (Sandbox Code Playgroud)
问题: 我想读取视图并将整个视图存储到变量中:
$html = file_get_contents($url);
Run Code Online (Sandbox Code Playgroud)
echo $ url是http:// localhost:8000/playlist/raw/1这是对的.
但这不起作用.我得不到任何回应,而php工匠服务休息.为什么?这有什么不对?
我的场景如下:
如果用户从“maxRedemptionForDiscount”中选择 true 并在“maxRedemptionForDiscountValue”中输入“0”,则应该有一条错误消息呈现到特定字段(在 TextType 字段的位置)
这是带有事件监听器的形式:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add(
'maxRedemptionForDiscount',
ChoiceType::class,
[
'placeholder' => false,
'multiple' => false,
'choices' => [
true => 'discount.form_fields.set_max_redemptions',
false => 'discount.form_fields.unlimited',
],
'label' => 'discount.form_fields.max_redemption_for_discount',
'translation_domain' => 'entities',
'required' => false,
'error_bubbling' => true,
'attr' => [
'class' => 'maxRedemptionForDiscountSelect',
],
]
)->add(
'maxRedemptionForDiscountValue',
TextType::class,
[
'label' => 'discount.form_fields.set_max_redemptions',
'translation_domain' => 'entities',
'required' => false,
]
)->addEventListener(
FormEvents::PRE_SUBMIT,
[$this, 'onPreSubmit']
);
}
Run Code Online (Sandbox Code Playgroud)
这是 onPreSubmit 函数:
/**
* @param …Run Code Online (Sandbox Code Playgroud) 在 Symfony 文档中有这样的条目:
https://symfony.com/doc/current/mailer.html#email-addresses
...您可以将多个地址传递给每个方法:
$toAddresses = ['foo@example.com', new Address('bar@example.com')];
$email = (new Email())
->to(...$toAddresses)
->cc('cc1@example.com', 'cc2@example.com')
// ...
Run Code Online (Sandbox Code Playgroud)
;
这是我的数组:
$recipients = [
'test1@test.de',
'test2@test.de'
];
Run Code Online (Sandbox Code Playgroud)
当我尝试像这样发送时:
$recipients = [
'test1@test.de',
'test2@test.de'
];
$email->to($recipients);
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
An address can be an instance of Address or a string ("array") given).
Run Code Online (Sandbox Code Playgroud)
这里有什么问题吗?好的 - 让我们尝试用字符串发送它:
当我尝试像这样发送时:
$recipients = "test1@test.de,test2@test.de";
$email->to($recipients);
Run Code Online (Sandbox Code Playgroud)
我收到另一个错误:
电子邮件“test1@test.de,test2@test.de”不符合 RFC 2822 的地址规范。
任何人都可以解释如何使用 symfony 邮件程序将电子邮件发送到多个->to()地址吗?
在我从 bootswatch ( https://bootswatch.com/flatly/ )安装平面模板后,twbs 的正常模式窗口不再工作。
<a data-toggle="modal" href="#" data-target="#myModal">CLICK!</a>
.
.
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body">
...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在我将主题添加到我的应用程序中之前,它工作得很好。
这意味着模式窗口会出现,但并未处于活动状态。模态窗口出现在灰色背景后面。模态窗口没有焦点。我也关不掉 但为什么?
css modal-dialog twitter-bootstrap bootswatch bootstrap-modal
如何使用 dql 查询获得随机结果?
这是我的查询:
$firstCategoryId = 50;
$repository = $this->entityManager->getRepository(BaseProduct::class);
$products = $repository->createQueryBuilder('p')
->join('p.categories', 'c')
->where('c.id = :categoryId')
->setParameter('categoryId', $firstCategoryId)
->getQuery()
->setMaxResults(4)
->getResult();
Run Code Online (Sandbox Code Playgroud)
这总是让我返回前 4 个产品。假设 ID 为 50 的类别有 100 多种产品。我想要的是从 ID 为 50 的类别中随机查询 4 篇文章。但是如何?这可能吗?当然,我可以不设置 Max Result 而不是用 PHP 来设置……但由于性能原因,这不是一个好的解决方案。
php ×11
symfony ×7
laravel ×4
doctrine-orm ×3
eloquent ×2
mysql ×2
api ×1
arrays ×1
bootswatch ×1
command ×1
console ×1
css ×1
events ×1
forms ×1
modal-dialog ×1
model ×1
process ×1
relationship ×1
token ×1
validation ×1