JMS Serializer按某种条件添加组

时间:2018-03-27 10:48:50

标签: symfony jmsserializerbundle

我有实体 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == Keys.Enter) { // dataGridView1.EndEdit(); if (dataGridView1 != null && dataGridView1.SelectedCells != null && dataGridView1.CurrentCell != null) // && !dataGridView1.ReadOnly) { dataGridView1.EndEdit(); //dataGridView1.CurrentCell.ReadOnly = true; //dataGridView1.CurrentCell.ReadOnly = false; if (!(dataGridView1.RowCount == 10 && AllRowCellsEntered(dataGridView1, 9))) { bool allEntered = true; int col = dataGridView1.CurrentCell.ColumnIndex; int row = dataGridView1.CurrentCell.RowIndex; //dataGridView1.EndEdit(); //check if all values in the current row are entered allEntered = AllRowCellsEntered(dataGridView1, row); if (allEntered) { if (row == dataGridView1.Rows.Count - 1) { dataGridView1.Rows.Add(1); dataGridView1.Rows[row].Cells[0].Value = row + 1; dataGridView1.Rows[row + 1].Cells[0].Value = row + 2; dataGridView1.Rows[row].ReadOnly = true; dataGridView1[0, row + 1].ReadOnly = true; dataGridView1.CurrentCell = dataGridView1.Rows[row + 1].Cells[1]; return true; } else { dataGridView1.CurrentCell = dataGridView1[1, dataGridView1.Rows.Count - 1]; return base.ProcessCmdKey(ref msg, keyData); } } else if (col == dataGridView1.Columns.Count - 1) { MessageBox.Show("you are in the last cell and not all values are entered,\n enter all values please!"); } else { dataGridView1.CurrentCell = dataGridView1[col + 1, row]; } } else { //dataGridView1.CurrentCell=null; } } } return base.ProcessCmdKey(ref msg, keyData); } private bool AllRowCellsEntered(DataGridView dataGridView, int row) { bool allEntered = true; for (int i = 1; i < dataGridView.Columns.Count; i++) { if (dataGridView.Rows[row].Cells[i].Value != null) { int result; if (!(int.TryParse(dataGridView.Rows[row].Cells[i].Value.ToString(), out result))) { allEntered = false; // dataGridView.Rows[row].Cells[i].Value = ""; break; } } else { allEntered = false; break; } } return allEntered; } private void Form1_Load(object sender, EventArgs e) { dataGridView1.Rows.Add(); dataGridView1[0, 0].Value = 1; dataGridView1[0, 0].ReadOnly = true; dataGridView1.CurrentCell = dataGridView1[1, 0]; } private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) { if ((dataGridView1.CurrentCell != null)) { if ((dataGridView1.CurrentCell.Value != null)) { string _cellValue = dataGridView1.CurrentCell.Value.ToString(); int _parsedCellValue = 0; { if (!(Int32.TryParse(_cellValue, out _parsedCellValue))) { dataGridView1.CurrentCell.Value = null; } } } } if (dataGridView1.Rows.Count == 10) { if (AllRowCellsEntered(dataGridView1, dataGridView1.Rows.Count - 1)) { dataGridView1.ReadOnly = true; dataGridView1.CurrentCell = null; MessageBox.Show("done!"); } } } ,当我的实体包含某些条件时,我想为序列化程序添加其他组,我为Notification创建事件serializer.pre_serialize,并在检查条件后在序列化上下文中添加其他组,但作为回应,它不起作用。示例我想要添加一些嵌套到我的Notification它将是另一个实体,例如Notification并且这个实体有一些组,并且在我创建上下文Question之后设置它一切正常,但是当我尝试在SerializationContext::create()中添加其他组时,它不起作用

这是我的实体

serializer.pre_serialize

和我在事件中添加的另一个实体

class Notifications
{
use TraitTimestampable;

const GROUP_POST_NOTIFICATION = 'post_notifications';
const GROUP_GET_NOTIFICATIONS = 'get_notifications';

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @Annotation\Groups({
 *     "get_notifications"
 * })
 */
private $id;

/**
 * @var object
 *
 * @Annotation\Groups({
 *     "get_notifications"
 * })
 */
private $providerEntity;

但作为回应我有

class Questions implements NotificationInterface
{
use TraitTimestampable;
const GROUP_POST = 'post_question';
const GROUP_GET = 'get_question';

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @Annotation\Groups({
 *     "get_question"
 * })
 */
private $id;

    /**
 * @return array
 */
public static function getGetGroup()
{
    return [self::GROUP_GET];
}

但作为回应我有

public function onPreSerializeNotifications(PreSerializeEvent $event) 
{
    /** @var Notifications $notifications*/
    $notifications = $event->getObject();
    $provider = $notifications->getProvider(); //this is Questions::class
    $this->providerEntity = $this->entityManager->getRepository($provider)
        ->findOneBy(['id' => $notifications->getProviderId()]);
    $notifications->setProviderEntity($this->providerEntity);

    $attr = $event->getContext()->attributes;
    $groups = array_merge($attr->all()['groups'], $provider::getGetGroup());
    $attr->set('groups', $groups);
}
为什么?

0 个答案:

没有答案