排行榜中显示的数据缓慢

时间:2013-12-19 14:09:09

标签: php mysql symfony

我在从数据库方案中检索数据并将其显示在网格中时遇到了一些麻烦,这是我使用的sql方案: enter image description here

观点:

{% extends 'MyBundle:Default:index.html.twig' %}
{% block body %}

    <table class="table table-striped table-bordered">
        <tr>
            <th>Player name</th>
            {% for action in actions %}
                <th>{{ action.displayName }}</th>
            {% endfor %}
        </tr>
        {% for stat in stats %}
            <tr>
                <td>{{ stat.playername }}</td>
                {% for key, action in actions %}

                    {% for key2, a in stat.actions|group('id', stat.id) if (key2 == (key + 1)) %}
                        <td>{{ a|length }}</td>
                    {% else %}
                        <td>0</td>
                    {% endfor %}

                {% endfor %}

            </tr>
        {% else %}
            <p>No statistics have been found</p>
        {% endfor %}
    </table>
{% endblock %}

group函数用于对每个用户的数据进行分组,这是我写的函数: namespace MyName \ MyBundle \ Twig;

class GroupExtension extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('group', array($this, 'arrayGroup')),
        );
    }


    public function arrayGroup($array, $element, $playerid)
    {
        $outputArr = array();

        foreach($array as $key => $value)
        {
            if($value->getPlayer()->getId() == $playerid)
            {
                $outputArr[$value->getAction()->getId()][] = $value;
            }
        }

        return $outputArr;
    }
    /**
     * {inheritDoc}
     */
    public function getName()
    {
        return 'group_extension';
    }
}

一切都有效,但加载速度非常慢(我用虚拟记录填充表格) 我认为这是因为我在玩家实体中创建了一个oneToMany关系,以便轻松地对所有内容进行分组。我只是不知道其他任何可能性。

这是玩家实体

use Doctrine\ORM\Mapping as ORM;

/**
 * Player
 *
 * @ORM\Table(name="player")
 * @ORM\Entity
 */
class Player
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="NONE")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="playerName", type="string", length=17)
     */
    private $playerName;

    /**
     * @var PlayerAction[]
     *
     * @ORM\OneToMany(targetEntity="PlayerAction", mappedBy="player")
     */
    private $actions;


    /**
     * Set id
     *
     * @param integer $id
     * @return Player
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set playerName
     *
     * @param string $playerName
     * @return Player
     */
    public function setPlayerName($playerName)
    {
        $this->playerName = $playerName;

        return $this;
    }

    /**
     * Get playerName
     *
     * @return string 
     */
    public function getPlayerName()
    {
        return $this->playerName;
    }

    /**
     * @param mixed $actions
     */
    public function setActions($actions)
    {
        $this->actions = $actions;
    }

    /**
     * @return mixed
     */
    public function getActions()
    {
        return $this->actions;
    }
}

2 个答案:

答案 0 :(得分:0)

一切正常意味着您的代码是正确的。也许你需要检查你的表结构,你可以运行命令:

mysqldump -d your_database_name 

并在此处分享输出。

答案 1 :(得分:0)

我修好了, 这是我正在执行的查询:

$queryActions = $em->createQuery(
    "SELECT a.actionName, a.id, a.displayName
    FROM MyBundle:Action a");

$actions = $queryActions->getResult();
$query = $em->createQuery(
    "SELECT p.playerName, a.actionName, SUM(a) as amount
     FROM MyBundle:Player p
     LEFT JOIN p.actions pa
     INNER JOIN pa.action a
     GROUP BY pa.action, p
     ORDER BY p.playerName ASC
     "
);

$result = $query->getResult();

给了我一个我可以使用的结果集,我只是操纵它,所以它可以给我一个很好的概述和良好的结果集来处理树枝模板

$stats = array();
foreach($result as $record)
{
    $stats[$record["playerName"]][] = $record;
}


$uarray = array();

foreach($stats as $playerstat)
{
    # set all actions to 0 first, this way we prevent non sync stats
    foreach($actions as $action)
    {
        $uarray[$playerstat[0]['playerName']][$action['actionName']] = 0;
    }

    # now set it's good stats
    foreach($playerstat as $actionstat)
    {

        //$uarray[$actionstat['playerName']]["playerName"] = $actionstat['playerName'];

        $uarray[$actionstat['playerName']][$actionstat['actionName']] = $actionstat['amount'];
    }
}

$data['stats'] = $uarray;
$data['actions'] = $actions;

我希望这对某人有用:)